我似乎在使用Apache 2.2的Rewrite模块(在FreeBSD 8上运行)时遇到了麻烦。 我的.htaccess文件如下所示:
Options -Indexes RewriteEngine on RewriteRule ^xpaste$ cross.php [L] RewriteRule ^x([a-f0-9]*)$ cross.php?id=$1 [L] RewriteRule ^(gen|[a-f0-9]+)$ index.php?$1 [L] RewriteRule ^(.*)$ index.php [L]
然而不知何故,最后一条规则超越了其他三条。 一旦我发表评论,其他规则按预期工作:
www.mysite.com/xpaste打开“cross.php”
www.mysite.com/x132633打开“cross.php?id = 132633”
www.mysite.com/gen打开“index.php?gen”
我已经尝试了各种RewriteRule上的L,NS和S = n标志的各种组合,但除非我注释掉最后一条规则,否则一切都会被引导到index.php。 一旦发现匹配,我该如何让Apache停止处理RewriteRules?
RewriteRule生成一个INTERNAL REDIRECT,然后从第一个RewriteRule(RewriteRule ^ xpaste $ cross.php [L])开始重复处理。 你需要在最后一个RewriteRule之前添加RewriteCond:
RewriteEngine on RewriteRule ^xpaste$ cross.php [L] RewriteRule ^x([a-f0-9]*)$ cross.php?id=$1 [L] RewriteRule ^(gen|[a-f0-9]+)$ index.php?$1 [L] RewriteCond %{REQUEST_URI} !^/(cross|index)\.php$ RewriteRule ^(.*)$ index.php [L]
NS最后的规则应该已经工作。 也可以在其他规则上做,没有任何伤害。 我正在到我所在的地方[L,NS],在有L的地方添加NS。
RewriteEngine on RewriteRule ^xpaste$ cross.php [L] RewriteRule ^x([a-f0-9]*)$ cross.php?id=$1 [L] RewriteRule ^(gen|[a-f0-9]+)$ index.php?$1 [L] RewriteRule ^(.*)$ index.php [L,NS]
我还没有testing过,但这里也有一个有趣的方法,对于更通用的解决scheme,使用IS_SUBREQvariables。 https://stackoverflow.com/questions/9555247/can-anyone-think-of-when-a-sub-request-rewrite-is-useful
这篇文章也提到了REDIRECT_STATUSvariables。 http://www.webmasterworld.com/apache/4299003.htm