How to Use a htaccess Redirect to www
- 0 Comments
Did you know there are likely four ways to access your site’s home page? It’s not a good thing but you can eliminate the problem by using a htaccess redirect to www.
Most likely, your site can be accessed through each of the following URLs:
http://www.yourwebsite.com
http://yourwebsite.com
http://www.yourwebsite.com/index.php
http://yourwebsite.com/index.php
Now, we can’t control how other people link to our sites and often times we may forget ourselves and link using varying methods. When this happens, two problems can occur. First, imagine having 100 links to your site spread out equally among the four URLs listed above. In Google’s eyes, that would be four different pages with only 25 links each. Now multiply this scenario times every page in your site. Secondly, not only do you have a backlink problem, you also have a duplicate content problem as Google is indexing multiple copies of the same page.
You’ve likely heard of the duplicate content penalty. If you’ve not, basically Google gives the most value to the original bit of content. Subsequent content which is identical to the original is given a lesser value. To fix this problem, you need to canonicalize your site. This is just a fancy way of telling your server what your preferred domain is and redirecting non-preferred requests to the preferred one.
Your preference is just that… YOUR preference. If you prefer having the www, then redirect to www. If not, you can redirect to the non-www version. I’m not aware of any concrete data that shows an SEO advantage for selecting one way over the other.
As far as Google is concerned, you can prevent it from indexing multiple copies of your pages by using Google Webmaster Tools. If you have submitted your site to GWT all you need to do is go to your dashboard and click on your domain name. In the left sidebar, click on “tools”, then click on “set preferred domain”. Here, you can choose between www or no www. This solution only works for Google’s index though. It does nothing to solve the problem for other search engines such as Yahoo or MSN. To do this, you need to tackle the problem in your .htaccess file.
To solve the problem for all Internet traffic, you need a htaccess redirect to www. All you need to do is edit your .htaccess file by adding a bit of code (this code is specifically for servers running PHP). Your .htaccess file is located in the root folder of your domain in your hosting control panel. Open your hosting control panel and find a file named .HTACCESS. If you cannot find it, contact your host. Open up your .htaccess file and edit it. The method to do this will vary with differing control panels, but you should be able to click on the file and then click on a link to edit or open the file. If using an FTP program, you may be able to right click on your file and select view or edit. Once you have the file open, copy and paste the following code to a blank line and save the file. Now you should be able to type in all four of the URLs listed above and end up at http://www.yourwebsite.com every time. Try it and see.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/ [NC]
RewriteRule ^(.*)index.php?$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule (.*) http://www.yourdomain.com/$1 [L,R=301]
NOTE: DO NOT FORGET TO REPLACE YOURDOMAIN.COM WITH YOUR SITE’S NAME AND EXTENSION (.COM,.NET,ETC.)!
The above code is to redirect to www. For a redirect to non-www, enter the following instead:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/ [NC]
RewriteRule ^(.*)index.php?$ http://yourdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.yourdomain\.com [NC]
RewriteRule (.*) http://yourdomain.com/$1 [L,R=301]



