When building a website, from time to time it’s necessary to redirect the non www. version of the website to the www. version, especially since you’re then less likely to have two versions of your website in the Google SERPS.

And since a web client of mine is asking how this is done, I’ll share the most common method, a few simple lines in your .htaccess file in the website’s root folder.

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

Pretty simple, huh? And for completion, here’s the redirect from www. to the non www. version.

#REDIRECT TO NON WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} !^web-programmer\.com$ [NC]
RewriteRule ^(.*)$ http://web-programmer/$1 [R=301,L]

That’s really all there is to it. Tidy, useful and consistent. Everything a web development project should be.