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). 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. 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! February 08, 2011
About the Author:

Joseph is the lead developer of Vert Studios Follow Joseph on Twitter: @Joe_Query
Subscribe to the blog: RSS
Visit Joseph's site: joequery.me