Rewriting URLs to force www or non-www URLs

CODE:
  1. #Activate the rewrite engine if it isn't so already
  2. RewriteEngine on
  3. #intercept all HTTP requests to the site that do not have a 'www.' at the beginning of the Domain name
  4. #and then force a redirect to the same page address only this time, including the 'www.'
  5. RewriteCond %{HTTP_HOST} !^www\..* [NC]
  6. RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
  7. #The inverse of the above rule - intercept HTTP requests with a 'www.' at the start and redirect to the non-www version
  8. # Ensure that your serverName directive is set up to the non-www name of your domain
  9. RewriteCond %{HTTP_HOST} ^www\..* [NC]
  10. RewriteRule ^(.*) http://%{SERVER_NAME}/$1 [R=301]

Leave a Reply

Your email address will not be published. Required fields are marked *

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>