.htaccess仅在满足两个条件时才执行规则

时间:2014-02-08 04:20:43

标签: .htaccess mod-rewrite

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/((\w+))?$ ?page=$1&other=$3 [L]
RewriteRule ^(\w+)(/(\w+))?$ ?page=$1&other=$3 [L]

这样做是因为如果用户输入 mysite.com/blarg 并且/ blarg不是真正的目录OR文件,它会发送给 index.php ?页= blarg

但是当我去 mysite.com/iphone (这是一个真正的目录,但不是文件)时,我被引导到 mysite.com/iphone/?page=iphone&其它=

1 个答案:

答案 0 :(得分:0)

这是因为最后RewriteRule行与RewriteCond存在。请注意,RewriteCond仅适用于下一个RewriteRule

使用此代码:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(\w+)/((\w+))?$ ?page=$1&other=$3 [L]
RewriteRule ^(\w+)(/(\w+))?$ ?page=$1&other=$3 [L]