There are quite a few ways to automatically redirect your site visitors from a non-www to a www URL on your website. In the past, I’ve always setup a ServerAlias in apache and these ReWrite rules to accomplish this.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.somedomain.com$
RewriteRule ^/(.*) http://www.somedomain.com/$1 [R=301]
Recently, I’ve run into this more and more and prefer an easier solution.
Case in point: you are using Amazon ELB to host your site on a few backend webserver instances. Amazon gives you a hostname which is the public address of your cluster of webservers (something like yourloadbalancer-1234567890.us-east-1.elb.amazonaws.com) The problem is you can’t set your root domain to use a CNAME.
Solution: Use Millcreek Systems’ free www redirect service.
- In DNS, set your root domain to point to 75.101.129.58
- In DNS, set www to point to the real location of your site, the CNAME of your ELB or even an A record for your server(s) IP address(es).
- Sit back and enjoy.