Saturday, November 27, 2010

Adding www to your domain with htaccess

The script below adds www to all the requests without the prefix www e.g. example.com and converts it to www.example.com

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

Line one -> Switch on Rewrite Engine
Line two -> Looks for url without www and captures the rest of url
Line three -> Add the www to previous captured url and forwards the request