[英]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/并单击“注销繁荣” 。
出现此问题是因为我有一个Zend Framework Application。 当我访问http://some-site.somewhere.net/时,它会显示索引页,但需要“控制器”和“操作”的按钮不起作用。 它们在http://some-site.somewhere.net/index/中可以正常工作。 试图添加到.htaccess
RewriteRule ^/index/?$ / [NC,L,R]
也尝试过
RewriteRule ^/?$ /index/ [NC,L,R]
如以下链接Zend Framework 2 Rewrite 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>
所以,如果我点击第一个表格上注销它发送一个POST
到请求/Auth/logout/
第二种形式发送POST
请求/
。
因此,从两个注销按钮获得不同的行为似乎不合逻辑。
我建议您为注销提供一种解决方案,并在您的应用程序中一致地使用它。
您还应该问自己,使用表单和POST
方法注销是否有用。 我认为应该做一个简单的链接( GET
请求)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.