Redirect to www with .htaccess
Written by Shawn S on July 16, 2008
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 redirect to www with .htacess.
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. I’m not aware of any valid data that shows an SEO advantage for selecting one way over the other.
You can prevent Google from indexing multiple copies by using Google Webmaster Tools. If you have submitted your site to GWT, 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. To do this, you need to tackle the problem in your .htaccess file.
To redirect to www for all Internet traffic, you need to modify 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. 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]
Posted in: SEO, Webmaster Tools











If you’re a webmaster, you want to see your site as high as possible in the search engine results pages. The higher your site gets, the more traffic you can expect, and obviously more traffic leads to more buyers. Without traffic, it doesn’t matter how pretty your site is or how valuable the content is, as you just aren’t going to 



