Forum Moderators: phranque

Message Too Old, No Replies

Rewrite not working as expected

         

barns101

6:16 pm on Sep 6, 2006 (gmt 0)

10+ Year Member



EDIT: The title should read "Rewrite not working as expected"

I'm rewriting seemingly static URLs onto a script to display results from a database and have the following .htaccess file:


[quote]# Redirect any pubs whose names or locations have changed
redirect 301 /pubs/loxley/robin-hood/ http://www.example.com/pubs/stannington/robin-hood/

# Rewrite the area and name onto the new script to find the pub
RewriteRule ^([a-z-]+)/([a-z'0-9&\.-]+)/$ pubs.php?area=$1&name=$2 [NC,L]
[/quote]

So for example http://www.example.com/pubs/stannington/robin-hood/ is rewritten to http://www.example.com/pubs/pubs.php?area=stannington&name=robin-hood

That works fine.

Sometimes I need to change the name or area of an existing pub and so I use the 301 redirect at the top of the script. Strangely though, I noticed that the example shown gets redirected from http://www.example.com/pubs/loxley/robin-hood/ to http://www.example.com/pubs/stannington/robin-hood/?area=loxley&name=robin-hood

So it does get redirected to the correct URL, but the second part of my redirection appends the variables to the URL. Both sets of code work exactly as expected when used separately.

Any ideas why it behaves in this way?

Caterham

7:07 pm on Sep 6, 2006 (gmt 0)

10+ Year Member



Don't mixup mod_rewrite and mod_alias in per-dir context, this creates often chaos. Use mod_rewrite to force the redirect instead:

RewriteRule ^loxley/robin-hood/$ http://www.example.com/pubs/stannington/robin-hood/ [R=301,L]

# Rewrite the area and name onto the new script to find the pub
RewriteRule ^([a-z-]+)/([a-z'0-9&\.-]+)/$ /pubs/pubs.php?area=$1&name=$2 [NC,L]

[edited by: Caterham at 7:13 pm (utc) on Sep. 6, 2006]

barns101

7:38 pm on Sep 6, 2006 (gmt 0)

10+ Year Member



That worked perfectly, thanks! :)