Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite not working as expected

Simple rewrite rule works differently on different servers.

         

barns101

2:16 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



I have the following mod_rewrite rule using .htaccess working as expected on one website running Apache 1.3.34 (Unix):


RewriteEngine on
RewriteRule ^([A-za-z]+)$ /offers/$1/ [R]
RewriteRule ^([A-za-z]+)/ offers.php?types=$1

If a visitor goes to [my-url.com...] they get redirected to [my-url.com...] (adding the trailing slash) and this pulls information from [my-url.com...]

However, on my other website running Apache 1.3.33 (Unix) it works, but the visitor gets redirected to [my-url.com...] (the www. is missing).

Any idea why the difference and how I can get the "www." back? I guess I could hard code the whole URL including the "www." But there must be a simpler way!

Thanks in advance :)

jdMorgan

1:02 am on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try adding the canonical domain name to the external redirect rule:

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

It's good practice to always do this, in case your ServerName is non-www and UseCanonicalName is set to 'on'.

The [NC] flag makes the alphabetic character compare non-case-sensitive, and so eliminates some uneccessary CPU work.

Jim

barns101

5:50 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Thank you for the advice, it worked a treat (and has taught me about case insensitivity with mod_rewrite!) :)

I still don't know why the original code worked differently on two almost identical Apache servers. :(

jdMorgan

2:32 am on Mar 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either the ServerName & ServerAlias or the setting of UseCanonicalName are probably different on the two servers.

Jim

barns101

1:06 pm on Mar 17, 2006 (gmt 0)

10+ Year Member



Thanks Jim, and once again you were right. One server name included the "www." and one didn't.