I am using this rule:

RewriteRule !^(.*?/.*|.*?\.(?:php|html)$)$ headers.php?a=$1 [L]

(based on the great contributions on http://stackoverflow.com/questions/3345747/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!

Accepted Answer

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]
Written by bcat
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki