Add www subdomain automatically to your domain with htaccess

Posted by Danny Herran on Aug 9, 2010 in Backend | No comments

Sometimes we need to add the www to our domain automatically, maybe for aesthetics or ajax issues across subdomains. Apache .htaccess is our friend here. You just need to add this to your existing .htaccess file or just make a new one with the following contents:

RewriteCond %{HTTP_HOST} ^your-domain\.com [NC]
RewriteRule ^(.*)$ http://www\.your-domain.com/ [L,R=301]