Friday, January 28, 2011

Redirect all incoming root domains to www

Put the following in your default virtual host if you want to redirect all of your root domains to your www subdomain.

Add any additional tlds to the list if you need them, uncomment the RewriteLog directives to see if it's working right.

I wrote this for Amazon EC2 to work with their load balancer, which requires you use a cname and using a cname for the root domain is invalid in DNS.

RewriteEngine on
#RewriteLog "/var/log/httpd/rewrite.log"
#RewriteLogLevel 3
RewriteCond %{HTTP_HOST} ^([^\.]+\.(com|net|org|biz|ca|info|co\.uk))$
RewriteRule (.*) http://www.%1$1 [R=301,L]

Michelle Trusty