Search engines are making duplicate content an easy problem to solve. This article outlines the htaccess method I use to redirect my sub domains to www.

I like to forward all my domains to the www sub-domain, here are a few reasons why:

  • Eliminate duplicate content in search engines
  • Remove confusion in CMS Control panels and sessions

The code

This is the code I place in my htaccess file.

RewriteEngine On
RewriteBase /

# Redirect sub-domains to www
# v1.2
# Last Updated: June 12, 2010

#rewritecond %{HTTP_HOST} !^dev\.nickyeoman\.com$ #same .htaccess on dev server (optional)
rewritecond %{HTTP_HOST} !^www\.nickyeoman\.com$ [NC]
rewriterule ^(.*)$ http://www.nickyeoman.com/$1 [R=301,L] 

Please note the exclamation mark means not, the backslashes escape the periods, and the NC means case-insensitive. L means last rule (stop rewriting after this).

Logical And

I do web development on my local machine and then set a sub-domain (dev.mydomain.com) using the etc/hosts file. The above code snippet above does a logical and until the rewriterule is reached. In my opinion you should list all the sub-domains you plan on using there so you don't get a 301 if Apache is not configured correctly.

Reference

Comments

Show/Hide Comment form