Apache 301: Force www, Redirect Index to Root
Backlinks are the foundation of SEO. If you spent hours trying to get a particular backlink, it’d be extremely discouraging to know that you somehow lost a portion of the credibility associated with that link. This can happen when a website’s URLs are not canonicalized/normalized.
The Code
This .htaccess code redirects http://domainname.com to http://www.domainname.com. Additionally, it forces any call to index.php to the domain root. (This code snippet assumes you’re using PHP extensions. If you’re using HTML extensions, just replace each instance of PHP with HTML.)
#redirect index.php to root
Options +FollowSymLinks
DirectoryIndex index.php
# force www in url
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
# END force www in url
#rewrite index.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.yourdomain.com/ [R=301,L]
#END rewrite index.php
The Concept
In my opinion, the most consistent way to normalize URLs is through Apache 301 Redirects. However, the recent advent of the rel=canonical tag has provided an alternative to the development-heavy Apache 301s, but some remain skeptical on its effectiveness.
An Example
As an example, take Cloud929, a company that implements Joomla templates.
Currently, the following URLs are all valid. (After clicking them, the URL doesn’t change).
- http://www.cloud929.com/
- http://www.cloud929.com/index.php
- http://cloud929.com/
- http://cloud929.com/index.php
Note that both www and non-www versions as well as index.php are treated as separate web pages to the search engines. This could lead to duplicate content being indexed, potentially devaluing the significance of the content.
Contrast this with the Vert site, which uses the Apache .htaccess 301.
- http://www.vertstudios.com/
- http://www.vertstudios.com/index.php
- http://vertstudios.com/
- http://vertstudios.com/index.php
Notice that every link variation resolves to http://www.vertstudios.com.
Take Care of Your Content
You work hard to create content that drives traffic to your site. Take care of your content by ensuring links to your site have their maximum effect. If you have any questions about the URL normalization process, let us know in the comments section below!
Related Posts:


I was just working on 301s for a client this morning!
Well written and spot-on info!
On the same subject, how important do you think the nonWWW and WWW aspects is in terms of duplicate content?
^that was actually my next blog post :$
^ leading to this
http://googlewebmastercentral.blogspot.com/2008/09/demystifying-duplicate-content-penalty.html
When it comes to making your content viewable by Google, their blog is a decent source to use. When it comes to ranking or measuring effects, don’t listen to them. I’ve seen sites with some technical errors produce duplicate content, and their rankings did in fact suffer. On one site, an hour after fixing the duplicate content issues, the site jumped up for two of its keywords. There is of course a chance that the timing was just a coincidence, and that’ll take more data to verify.