Rewrite rule does not pass $1 when using !?
I am using this rule:
RewriteRule !^(.*?/.*|.*?\.(?:php|html)$)$ headers.php?a=$1 [L]
(based on the great 开发者_运维知识库contributions on Regex match this OR that?)
It rewrites to headers.php
when I type localhost/foo
but the a
variable is empty instead of foo
(I checked with var_dump($_REQUEST)
)
Any idea why? I tried using
RewriteCond %{REQUEST_URI} !headers
but it wasn't that.
Thank you!
The rule is negated, so it is executed if and only if the regular expression doesn't match the URI being processed. Since the capturing group doesn't match localhost/foo
, there's nothing for the regex engine to put into $1
. The solution is to avoid the use of negation in your RewriteRule
directive, and instead use RewriteCond
directives to check the negated regex. The following ruleset should work. (I haven't test it, though. It's possible that there's a mistake somewhere.)
RewriteCond %{REQUEST_URI} !/.*/
RewriteCond %{REQUEST_URI} !\.(html|php)$
RewriteRule (.*) headers.php?a=$1 [L]
精彩评论