有没有办法取回重写条件的引用传递给所有后续的重写规则?
这是一个例子:
RewriteEngine On RewriteCond %{HTTP_HOST} (bob).localhost [NC] RewriteRule ^/where/? /index.php\?user_name=%1 RewriteRule ^/who/? /index.php\?user_name=%1
在这个例子中,我期望这个行为:
http://bob.localhost/where => http://bob.localhost/where/index.php?user_name=bob http://bob.localhost/who => http://bob.localhost/who/index.php?usern_ame=bob
但是对于第二条规则,我接收http://bob.localhost/who/index.php?user_name = 。
我已经尝试了几个不同的发行版使用Apache 2.2.17
条件只适用于下一个规则。 您需要重复该条件才能将其应用于其他规则:
RewriteCond %{HTTP_HOST} (bob).localhost [NC] RewriteRule ^/where/? /index.php\?user_name=%1 RewriteCond %{HTTP_HOST} (bob).localhost [NC] RewriteRule ^/who/? /index.php\?user_name=%1
在你的例子中,如果你的请求的HTTP_HOST值不匹配(bob).localhost ,你的请求将跳过^/where/? 规则(即使匹配),但可以使用^/who/? 规则。
如DerfK所示,rewritecond仅用于一个重写规则。
你可能会考虑一个备用的策略,然而,设置和阅读环境variables。
# this grabby rewrite will match anything, # *and* set 'bob' in a custom rewrite environment variable # it uses 'next' with the 'no sub requests' caveat to avoid loops RewriteCond %{HTTP_HOST} (bob).localhost [NC] RewriteRule ^(.*)$ - [env=host_uname:%1] [N,NS] # All these rules should then be evaluated, # in the 'next' pass - with the 'host_uname' env variable available RewriteRule ^/where/? /index.php\?user_name=%{ENV:host_uname} RewriteRule ^/who/? /index.php\?user_name=%{ENV:host_uname}