多重写规则
问题描述:
我想在我的.htaccess文件中有多个RewriteRules。多重写规则
# Enable Rewriting
RewriteEngine on
# Rewrite profile urls
# Input: /user<userId>
# Output: /profile.php?id=<userId>
RewriteRule ^user(\d+)/?$ profile.php?id=$1 [L]
# Rewrite by default to redirect.php
RewriteRule .* redirect.php
每个请求指向redirect.php
我想,在第一重写规则的[L]标志,将停止处理规则集。
答
将以下内容添加到第二个RewriteRule中。
# Rewrite by default to redirect.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* redirect.php
但我不知道,为什么这是必要的。
答
如果您想改写/user<userId>
到/profile.php?id=<userId>
并重写其他网址/redirect.php,那么你可以尝试这两个配置指令:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteRule .* /redirect.php
OR:
RewriteEngine on
RewriteRule ^user([a-zA-Z0-9_-]+)/?$ /profile.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /redirect.php
答
[L]标志仅在当前迭代中停止处理,但mod_rewrite重复处理直到路径不会保持不变。 在你的情况下,我认为你可以使用这样的规则:
RewriteRule ^user\d+$ profile.php
RewriteCond %{REQUEST_URI} !profile.php
RewriteRule ^.*$ redirect.php
** 1 **如果请求的文件名不是一个普通的文件,继续执行规则。 ** 2nd:**如果请求的文件名不是目录,请继续执行规则。 – 5ervant 2013-05-04 18:43:09