1

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!

Community
  • 1
  • 1
johnjohn
  • 4,221
  • 7
  • 36
  • 46

1 Answers1

2

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
  • 8,833
  • 3
  • 35
  • 41
  • Great idea! The sample code does not work, but you gave me a great lead to work on :) Thanks! – johnjohn Jul 27 '10 at 18:14
  • 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 Jul 27 '10 at 18:16
  • Thanks again, there 's still some error somewhere. It returns `The requested URL /foooo was not found on this server.` – johnjohn Jul 27 '10 at 18:18
  • 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 Jul 27 '10 at 18:22
  • 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. – johnjohn Jul 27 '10 at 18:36
  • It seems the problem lies on the first line. I am trying to mend it and will post again! – johnjohn Jul 27 '10 at 18:46
  • 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 Jul 27 '10 at 19:30
  • 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` – johnjohn Jul 27 '10 at 20:01
  • 1
    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 Jul 27 '10 at 20:14
  • Also, if it still doesn't work, make sure you have `AllowOverride all` set in the root `` block in the main configuration file (httpd.conf). – bcat Jul 27 '10 at 20:21
  • 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). – johnjohn Jul 27 '10 at 20:26
  • 1
    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 Jul 27 '10 at 20:29
  • 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! – johnjohn Jul 27 '10 at 20:35
  • 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 Jul 27 '10 at 22:44