在删除Zend 2.2中的htaccess后,Zend框架主页URL索引问题



我想在没有.htaccess的情况下运行应用程序,我删除了它。现在,当我点击主页,例如"localhost/testsite/index.php/"时,它可以正常工作,相关的链接url也可以正常工作。但当我尝试打开"localhost/testsite/index.php"时,它给出了以下错误消息。

出现404错误找不到页面。路由无法匹配请求的URL。

无异常可用

此外,当我打开"localhost/testsite/"时,主页会正常显示,但相关链接会停止工作。下面给出了我的应用程序模块.config.php。

return array(
'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'ZendMvcRouterHttpLiteral',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'ApplicationControllerIndex',
                    'action'     => 'index',
                ),
            ),
        ),
        // The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action
        'application' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/application',
                'defaults' => array(
                    '__NAMESPACE__' => 'ApplicationController',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),   
'controllers' => array(
    'invokables' => array(
        'ApplicationControllerIndex' => 'ApplicationControllerIndexController'
    ),
),
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

);

如果有人在zend 2.2中运行没有htaccess的应用程序,请提供帮助。

您可以添加一个Generic路由以使斜线可选,您可以使用分段路由类型来实现这一点

/**
 * Generic Route
 */
'generic' => array(
    'type'    => 'segment',
    'options' => array(
        'route'    => '[/:controller[/:action[/:id]]][/]', // allow trailing slash too [/]
        'constraints' => array(
            '__NAMESPACE__' => 'ApplicationController',
            'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
            'controller'    => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id'            => '[0-9]+',
        ),
        'defaults' => array(
            'controller' => 'Index',
            'action'     => 'index',
        ),
    ),
),

ZF2应用程序的起点位于public/index.php中,它不在发布主机的根文件夹中,因此我们需要.htaccess(使用apache的mod_rewrite)将所有浏览器请求重定向到[/]到[public/index.php],如果您有任何其他机制来模拟重定向,则可以绕过.htacccess文件。

最新更新