Non-www and www redirecting

Many web sites become unstuck with www and non-www redirecting - in some cases, they have even failed to set up the non-www version at all, but don’t have a redirect, leaving the visitor unsure whether the web site actually exists. In this short tutorial, I’ll tell you how to redirect the non-www address to the www address, and vice versa.

The non-www address is becoming increasingly used by web sites, as more and more advertise their site without the www. infront. For SEO purposes, it is best to choose to use either one or the other to get links to, rather than spreading the links over the 2, since some search engines, including Google, see the non-www site and the www site as 2 different web sites.

To redirect one to the other, you need to add a section of code to the .htaccess file, which is in the public_html directory of your web site. Be careful when editing anything in here, and do not delete or modify any code without understanding what it does.

To redirect the non-www address to the www address, (e.g. if someone types in danlprice.co.uk, direct them to www.danlprice.co.uk), enter the following code into your .htaccess file:

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

Be careful not to change anything apart from “yourwebsite” and “.com” if necessary. All this code does is add a Permanent 301 redirect to the address, which tells search engines that all links to the original address should be eventually counted as the second web site, and it also means that if visitors now type the non-www address into their browsers, they’ll be automatically taken to the www address.

If you wish to do the opposite of this, i.e. to redirect the www address to the non-www address, then enter this code in your .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^yourwebsite\.com
RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L]

Obviously, as said above, change “yourwebsite” and “.com” to your actual address, but don’t change any other parts of the code.

There you have it - you’ve now optimized the web address for SEO purposes, and for your visitors. I hope to write some tutorials for the .htaccess file in the future so stay tuned!

Leave a Reply