Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite/301 redirect question

Will the RewriteBase I used for one thing mess up my redirect?

         

Trisha

10:00 pm on May 24, 2005 (gmt 0)

10+ Year Member



I am setting up a 301 redirect for a site, but already have some other rewrite stuff in the htaccess file, including a: RewriteBase / - not sure if this will interfere with the redirect or not. I find all the rewrites, etc. confusing. This is what I have now and it works fine:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)shp/widgets/detail-nm/(.*)$ $1shp/widgets/detail-nm.php?product=$2
RewriteRule ^shp/gadgets/(.+)/?$ /shp/detail-gadgets.php?product=$1 [L]
(and a few more rewrite rules similar to the second one)

I need to add:

RewriteCond %{HTTP_HOST}!^www\.mydomain\.com
RewriteRule ^(.*)$ [mydomain.com...] [R=301,L]

Will the 'RewriteBase /' interfere with this? It looks to me like it would, but I don't understand this stuff that well. Which is why I didn't set up the redirects on all my sites yet - too scared of messing up something that isn't broken (yet anyway).

jdMorgan

11:08 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trisha,

Your RewriteBase is the default, and I doubt that it would affect your external redirect.

However, the only way to know is to test it. Make a backup of your 'safe' .htaccess, and if this new code messes things up, you can simply rename the backup to .htaccess, and be back online in seconds.

Since you are using a negative match, you'll need to add a condition to suppress the redirect if the client does not provide a Host header. Otherwise, you'll put the client and your server into an 'infinite loop' for all HTTP/1.0 clients (which do not provide a Host header). The new line simply checks to make sure the HTTP_HOST variable contains at least one character (i.e. is non-blank).


RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Jim

Trisha

11:49 pm on May 24, 2005 (gmt 0)

10+ Year Member



Thanks! This is what I did and it seems ok. Do you have any other suggestions for how to test it, other than viewing the pages and making sure they load ok? Possible looping was one of those things that has made me procrastinate in setting up the redirect in the past.

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^(.*)shp/widgets/detail-nm/(.*)$ $1shp/widgets/detail-nm.php?product=$2
RewriteRule ^shp/gadgets/(.+)/?$ /shp/detail-gadgets.php?product=$1 [L]
(and a few more rewrite rules similar to the second one)

[edited by: jdMorgan at 12:38 am (utc) on May 25, 2005]
[edit reason] Examplified. [/edit]