Zend Framework 2 .htaccess url issue


Zend Framework 2 .htaccess url issue

问题使用.htaccess使浏览器读取http://some-site.somewhere.net/index/像http://some-site.somewhere.net/.

发布网站示例访问http://test-site.6te.net/并尝试注册。重定向到身份验证页面后,返回http://test-site.6te.net/并尝试从下拉菜单中注销它不起作用。然后转到http://test-site.6te.net/index/并点击注销boom即可工作。

出现此问题是因为我有一个Zend Framework应用程序。当我访问http://some-site.somewhere.net/它出现在索引页上,但需要"控制器"answers"操作"的按钮不起作用。他们在http://some-site.somewhere.net/index/.尝试添加到.htaccess

RewriteRule ^/index/?$ / [NC,L,R]

也尝试过

RewriteRule ^/?$ /index/ [NC,L,R] 

如以下链接Zend Framework 2重写URL所示。同样在module.config.php中是这样的:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Test'Controller'Index' =>
            'Test'Controller'IndexController',
            'Test'Controller'Register' =>
            'Test'Controller'RegisterController',
            'Test'Controller'Login' =>
            'Test'Controller'LoginController',
            'Test'Controller'Auth' =>
            'Test'Controller'AuthController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'test' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        '__NAMESPACE__' =>'Test'Controller',
                        '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(),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            'test'       => __DIR__ . '/../view',
            'doctype'    => 'HTML5',
        ),
    ),
    'user-manager' => array(
        'type' =>'Segment',
        'options' => array(
            'route' => '/user-manager[/:action[/:id]]',
            'constraints' => array(
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'id' => '[a-zA-Z0-9_-]*',
            ),
            'defaults' => array(
                'controller' => ' Test'Controller'UserManager',
                'action' => 'index',
            ),
        ),
    ),
);

.htaccess

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::'2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

我不确定您试图用重写规则实现什么,但也许这个来自Zend Framework 2 Skeleton应用程序的htaccess文件可以帮助您。

否则,请详细解释不同重写规则的目的

编辑:

我按照你评论中的建议查看了你的网站

我认为您的问题与htaccess文件无关。您的htaccess文件似乎工作得很好,问题出在您的链接/表单操作中,所以请关注这一点


我做了一个账户。我登录了,然后转到主页。从那里注销不起作用。然后我试着去建议的http://test-site.6te.net/index,这给了我404,所以这条路线不存在。但后来我回到了http://test-site.6te.net/auth/,在那里注销操作很好。

我注意到以下差异:

auth路由上注销时,注销表单如下所示:

<form action="/Auth/logout/" method="post" name="Login" enctype="multipart/form-data" id="Login">                       
    <dd>
        <input type="submit" name="logout" class="btn btn-link" value="Log Out">
    </dd>
</form>

在主页面上注销时,注销表单如下所示:

<form action="/" method="post" name="LoginSm" enctype="multipart/form-data" id="LoginSm">                           
    <li>
        <button class="btn btn-link">
            <a href="auth/">
                <span class="glyphicon glyphicon-map-marker"></span>Restricted Area
            </a>
        </button>
    </li>
    <li class="divider"></li>
    <li>
        <dd>
            <input type="submit" name="logout" class="btn btn-link" value="Log Out">
        </dd>
    </li>
</form>

因此,如果我在第一个表单上单击登出,它会向/Auth/logout/发送POST请求第二表单向/发送POST请求。

因此,您从两个注销按钮获得不同的行为似乎并不合乎逻辑。

我建议您制定一个注销解决方案,并在应用程序中始终如一地使用它。

您还应该问问自己,使用表单和POST方法注销是否有用。我认为一个简单的链接(GET请求)就可以完成这项工作。