.htaccess subdomain redirect

I recommend

A common desire is to redirect all subdomains to a single domain for all website requests. The following rewrite rule will 301 redirect all subdomains other than www to the www subdomain.

RewriteCond %{HTTP_HOST} (?<!www\.)domain\.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

I used this rule in IIS 7.5 using HeliconTech's Ape, but I've tested it with Apache's mod_rewrite as well. It can go in your .htaccess file or httpd.conf. If put in httpd.conf, it will work as is using RewriteBase /, otherwise remove the trailing slash in the RewriteRule's substitution string.

I hope this helps :-)

Technical January 17, 2012

comments powered by Disqus