views:

38

answers:

1

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!

+2  A: 

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]
bcat
Great idea! The sample code does not work, but you gave me a great lead to work on :) Thanks!
john
I edited the code about a minute ago to fix a mistake; make sure you're testing the latest version. (I'm still not sure it works, though. I have no Apache server handy to test it on.)
bcat
Thanks again, there 's still some error somewhere. It returns `The requested URL /foooo was not found on this server.`
john
Oops, I think the slash character I had at the start of the rewrite rule shouldn't be there. Try the version I just put up.
bcat
I can't thank you enough for your efforts! Unfortunately, it is still not working. I am trying to modify it, I still haven't got it right though.
john
It seems the problem lies on the first line. I am trying to mend it and will post again!
john
Hmm, I've just tried it on my development server and it works fine. Are you sure you enabled mod_rewrite with a `RewriteEngine on` directive? If you have, and it still doesn't work, are you putting the rules in a .htaccess file, or the main Apache config file?
bcat
It is on and its on an .htaccess file. Does it play any role that I have put everything (and the script) on a subdirectory? like `localhost/script/.htaccess`
john
Addition: I also tried it on a linux server with the same results. The extra things on the file are:`Options +FollowSymLinks` `Options +Indexes` `RewriteEngine On`
john
Oh, I think it's because you're running the rules from a subdirectory! Apache strips the directory prefix from `RewriteRule` URIs, but it doesn't apply the same behavior to the `REQUEST_URI` variable. If you change the first line to `RewriteCond %{REQUEST_URI} !/script/.*/`, then everything should work properly.
bcat
Also, if it still doesn't work, make sure you have `AllowOverride all` set in the root `<Directory>` block in the main configuration file (httpd.conf).
bcat
Fantastic!! I was going mad :o) The only problem now (surprise!) is that it also redirects /script/ to /script/headers.php , which is undisired since my code is on index.php and I 'd like it to be called when it 's access like /script/ (the standard behaviour).
john
That part's easy. :) Change the pattern in the last line from `(.*)` to `(.+)`. This causes the pattern to match only when there is at least one character in the filename, so /script/ will no longer be matched, and therefore Apache's default index.php redirect will take over.
bcat
You are a GOD! Words can't express my gratitude for your helping me. I was going totally mad with this. I wish we could drink some beer together!
john
No problem, man. I was in a very similar boat a couple of months ago: I was stuck with a mod_rewrite problem I couldn't solve if my life depended on it. I love mod_rewrite because it's so useful, but actually writing rewrite rules is definitely a pain in the neck.
bcat