RewriteRule否决了第二条规则和GETs index.php?

时间:2013-06-18 20:21:52

标签: php .htaccess mod-rewrite

我有一个我认为有效的RewriteRule:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?lang=$1&page=$2
RewriteRule ^(.*)/editor/(.*)$ index.php?lang=$1&page=editor&func=$2

当我在第一级(lang和page)上执行$_GET['lang']时,它会返回语言。但如果我在第二级(lang,page和func)上尝试它,我会得到index.php

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:1)

第一场比赛后你没有[L]标志停止处理,你的第二条规则也会与第一条规则相匹配。由于它更具体,首先列出它,并包含[L]。此外,我建议使用(.*)来匹配下一个([^/]+)的所有内容,而不是贪婪/

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# List this first, with [L]
RewriteRule ^([^/]+)/editor/(.*)$ index.php?lang=$1&page=editor&func=$2 [L]
# Then URIs not containing editor/ will match the other rule
RewriteRule ^([^/]+)/(.*)$ index.php?lang=$1&page=$2 [L]

如果CSS&图像失败,您可能需要移动RewriteCond规则下的editor/,以确保条件仅适用于最不具体的全能匹配:

RewriteRule ^([^/]+)/editor/(.*)$ index.php?lang=$1&page=editor&func=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ index.php?lang=$1&page=$2 [L]