Forum Moderators: phranque

Message Too Old, No Replies

redirect non-www to www

is my code correct

         

proboscis

4:22 am on Mar 28, 2011 (gmt 0)

10+ Year Member




Hi,

This is supposed to redirect my index.shtml page to /

and redirect all non-www pages to the www version

Is it all correct?

Thanks!

---------------

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.shtml\ HTTP/
RewriteRule ^(([^/]+/)*)index\.shtml$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\. [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\:[0-9]+
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#

g1smd

7:31 am on Mar 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That will work, and it will fix all possible non-canonical variations.

In place of
shtml
I use
s?html? 
so that requests for index.shtml .html and .htm are all redirected.

In some circumstances the three conditions in the www canonicalisation rule can be simplified to:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ 


You code will work as is, so these suggestions are merely a matter of preference.

proboscis

8:10 pm on Mar 28, 2011 (gmt 0)

10+ Year Member



Good, thank you for the tips :) I didn't know I could do that.