htaccess中的规则冲突

时间:2013-04-17 09:25:12

标签: php .htaccess mod-rewrite url-rewriting rewrite

我正在使用htaccess文件的项目与我的班级合作,这个文件似乎不起作用。我有这个:

SetEnvIf Request_URI ^ root=/myroot/

RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index    
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$                      %{ENV:root}index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]

事实上,有三件事我不明白:

如果我将网址查询为/section/function/,我应该获得/myroot/index.php?type=section&action=function但我的浏览器中会显示localhost/section/function,从而导致错误。该网址是否已翻译为bidirectionnaly:myroot / index.php?type = section& action = function在浏览器中显示为/section/function/。如果我删除最后一行,我会得到空白错误页面,为什么会这样?

编辑:

与Olaf Dietsche所说的一致,我将规则改为

SetEnvIf Request_URI ^ root = / myroot /

RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index    
RewriteRule ^section/function/(.*)$ %{ENV:root}?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$                      %{ENV:root}?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]

但是当我点击<a href="section/somefunction"></a>时,我会看到一个页面localhost/section/some function。我真的不知道出了什么问题......

1 个答案:

答案 0 :(得分:0)

首先,你必须

RewriteEngine on

在.htaccess中使规则有效。

规则重写请求,但不要重定向客户端。两者之间的区别是

  • 重写将另一个页面的内容发送给客户端,而不告诉客户端。客户认为它获得了所请求页面的内容。
  • 重定向向客户端发送另一个网址,导致客户端使用此网址发出新请求。客户端知道(并在浏览器的栏中显示)它收到了另一页的内容。

如果要重定向客户端,则必须在RewriteRule

中添加R标记
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [R,L]

<强>更新

您必须在替换前删除%{ENV:root},因为该网址例如是/index.php而非/myroot/index.php

RewriteRule ^section[/]?$ /section/index    
RewriteRule ^section/function/(.*)$ /index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ /index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]