ReWriteEngine code to standardize on www URLs in apache

Submitted by donmc on Fri, 07/27/2012 - 17:46

If you want to redirect all requests to your site to the www version, [] all you need to do is add the following code to your .htaccess file:

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

This will redirect any requests to to There are several benefits from doing that:

  • It will avoid duplication in Google index caches because your site appears twice, once with the "www." and again without it.
  • Reliability of search indexing.
  • It's nicer, and more consistent.