.htaccess - redirect non www to www and retain subdomains from redirecting

Posted by RhymeGuy on Server Fault See other posts from Server Fault or by RhymeGuy
Published on 2012-05-23T13:37:06Z Indexed on 2012/11/12 5:02 UTC
Read the original article Hit count: 511

So, on my main domain 'domain.com' I created several subdomains from cPanel, like 'sub1.domain.com' and 'sub2.domain.com'. Their real location on server is in 'domain.com/sub1' and 'domain.com/sub2'.

Now, I want to redirect non www to www with .htaccess and this is what currently what i have:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301]
</IfModule>

This works. When somebody enter domain.com it will be redirected to www.domain.com. However when somebody enter sub1.domain.com, he will be redirected to www.domain.com/sub1 - which I don't want, it needs to be in sub1.domain.com.

What shall I add in .htaccess file to accomplish this?

© Server Fault or respective owner

Related posts about .htaccess

Related posts about redirect