Redirecting Non-WWW Domain to WWW Using htaccess
Many websites have canonical problems especially with their main domain URL.
According to Matt Cutts, Google Engineer, canonicalization is the "process of picking the best url when there are several choices, and it usually refers to home pages."
Cutts continues to explain that "…when Google “canonicalizes” a url, we try to pick the url that seems like the best representative from that set."
So for example, the search engines consider fabianlim.name, www.fabianlim.name and fabianlim.name/index.php to be different web pages, even though they all serve up the same content.
This results in a link popularity "split" between URLs and will affect the effectiveness of your search engine optimization efforts.
One solution to fix this problem is to perform a 301 redirect via the use of htaccess.
Two versions of the .htaccess solution exist, a generic and non-generic solution.
First, try the generic version because it does not require any modification of the code at all.
If the generic version does not work, you can try the non-generic version.
If your website already has a .htaccess file on the server, I strongly recommend first you create a backup version of the file on your local computer in case you mess up the codes accidentally.
So, to redirect the non-www version of your URL to the www version, copy and paste one of the two 3-line codes below into an empty text file and name the text file as htaccess.txt:
Version 1 (Generic Version)
(More user-friendly especially if you have multiple websites since you don’t need to insert your domain URL into the code below)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
Version 2 (Non-Generic Version)
(Be sure to change example.com to your domain name and be careful NOT to add/delete spaces or add/remove any of the symbols in the code)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Note: For either versions to work, Apache mod_rewite needs to be enabled by your web host
Upload this htaccess.txt file onto your web directory (typically public_html for cPanel servers).
Change the file name from htaccess.txt into .htaccess (i.e. place a dot in front of htaccess).
Test the results of this change by typing the non-www version of your domain using a web browser.
If the above steps have been implemented correctly, the non-www version will redirect to the www verion of your domain.
That’s all to it!
Popularity: 12% [?]













I just to know how you have .name? Still learning guy..
I think it’s up to our choice whether we set the canonical into WWW or non-WWW. but I still see many new website particularly from newbies that they don’t set one canonical to their website. I think you should take this issue to your training Fabian.
What if my directory already contains a file called .htaccess?
Hi Angeline,
If your directory already contains the .htaccess file, you can simply place the code on top of the other codes in the .htaccess file.
Hope this helps.
Fabian
Hi Fabian,
It doesn’t work when I add to existing code in the .htaccess file. I tried once on a WordPress site an again on a normal site but both do not work when adding to existing code.
Is there a solution?
Thanks and regards.
Hi Angeline,
For WordPress, you do not have to use .htacess to redirect non-www to www
All you need to do is login to your wp-admin, then click on:
‘Settings’ and check that the URL you typed in the following two areas are the www version of your domain:
WordPress address (URL)
Blog address (URL)
Click ‘Save Changes’ at the bottom of the page when you are done.
Hope this helps.
Fabian