Forum Moderators: phranque

Message Too Old, No Replies

Redirect dynamic script

mod_rewrite not working as expected

         

barns101

8:35 pm on Apr 30, 2006 (gmt 0)

10+ Year Member



I'm trying to redirect www.my-url.com/abc/search/list.php?letter=a (or any other letter) to www.my-url.com/abc/a/ with the following .htaccess file in the /search/ directory:


RewriteEngine on
RewriteRule ^list\.php\?letter=([a-z])$ http://www.example.com/abc/$1/ [NC,R=301,L]

I have no idea why it's not working, because I have used similar redirects on the same site without any problems. Maybe someone can spot something glaringly obvious that I have missed and put me out of my misery ;)

[edited by: jdMorgan at 10:27 pm (utc) on April 30, 2006]
[edit reason] Formatting & example.com [/edit]

jdMorgan

10:22 pm on Apr 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not visible to RewriteRule. RewriteRule can only see the URL, not the query data appended to it. You must use a RewriteCond examining %{QUERY_STRING} to solve your problem.

For a full run-down on rewriting, see the threads in Apache forum section of the WebmasterWorld library [webmasterworld.com]. The "Changing dynamic to static URLs" post demonstrates how to rewrite a URL with a query string, as part of the main subject.

Jim

barns101

10:32 pm on Apr 30, 2006 (gmt 0)

10+ Year Member



Thank you very much, I had no idea that query strings were not visible to RewriteRule, but I'm sure that I'll be able to sort it out now. :)

barns101

11:23 pm on Apr 30, 2006 (gmt 0)

10+ Year Member



Wow, that took some trial and error to finally figure out! ;)

For the benefit of anyone else wanting to do something similar, the following works:


RewriteEngine on
# Redirect old URLs to new ones
RewriteCond %{QUERY_STRING} ^letter=([a-z])$
RewriteRule ^list.php$ http://www.example.com/abc/%1/? [NC,R=301,L]