views:

100

answers:

1

I am trying to get Apache to redirect /a.php?a=123 to /b/123 (where 123 could be any number between 1 and 9999) but can't seem to get it to work.

This is what I have in htaccess:

RewriteEngine on

RewriteRule ^a.php?a=([0-9]+) /b/$1 [L]
RewriteRule ^a.php$ /c/ [L]

With this going to a.php?a=123 results in 404, but going to just a.php works as expected. I tried escaping the ? (RewriteRule ^a.php\?a=([0-9]+) /b/$1 [L]) but it still doesn't work.

What am I doing wrong please?

+7  A: 

The query string is not part of the URI path that is tested in the RewriteRule directive. This can only be tested with a RewriteCond directive:

RewriteCond %{QUERY_STRING} ^a=([0-9]+)$
RewriteRule ^a\.php$ /b/%1? [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^a\.php$ /c/ [L,R]

But if you want it the other way (requests of /b/123 are redirected to /a.php?a=123):

RewriteRule ^b/([0-9]+)$ a.php?a=$1 [L]
Gumbo
Thank you this does look like it should work, but now when I go to either "a.php?a=123" or just "a.php" both are redirecting me to /c/
Tim
It works now - I had missed out the ? at the end of the rule
Tim