htaccess redirect non-www to www version

Written by Peter Davies on .

This is pretty simple; in Joomla place the following code after the block where rewrites get turned on in the .htaccess file (nano .htaccess):

RewriteEngine On

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

On saving the file you will now have a site that only works from one sub-domain i.e. the correct main "www" one.