2/13/2008

Redirecting Canonical Hostnames with .htaccess

We needed to redirect any requests that do not start with www.strongwind89.com to make sure they include the www. We did this not only because it looks better, but to avoid the now common 301 sabotage.

Redirect: http://strongwind89.com/
To: http://www.strongwind89.com/

Redirect: http://mail.strongwind89.com/
To: http://www.strongwind89.com

Redirect: http://strongwind89.com/somefile.php
To: http://www.strongwind89.com/somefile.php

Solution:

Add the following directive:
RewriteCond %{HTTP_HOST} !^www\.seomoz\.org
RewriteRule ^/(.*) http://www.strongwind89.com/$1 [R=301,L]

Explanation:

This directive tells apache to examine the host the visitor is accessing (in this case: strongwind89.com), and if it does not equal www.strongwind89.com redirect them to www.strongwind89.com. The exclamation point (!) in front of www.strongwind89.com negates the comparison, saying “if the host IS NOT www.strongwind89.com, then perform RewriteRule.” In our case RewriteRule redirects them to www.strongwind89.com while preserving the exact file they were accessing in a back-reference.

No comments:

Live Page Popularity