Redirecting domain without redirecting subdomains

Avatar
  • Answered
Hiya,
First off thank you for your support pages they've helped us out a lot over the years!
We'd like to redirect our domain (we've got a non www to www redirect in place) but not redirect the subdomains so they'll still be found. This is what we have for our basic non www to www redirect:
# Redirect non www to www site
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

And so far to redirect the domain but not the subdomains I've got this:
# Redirect www site excluding subdomains
RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2|subdomain3|)\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com$1 [L,R=301]

I'd like your opinion as to whether or not a.) that code will work and b.) if that's actually the best way to do this.
Thank you!
Avatar
Arn
Hello, Thanks for the question about excluding your subdomains from a redirect. The main issue here will be WHERE you put the code in you .htaccess files, since the .htaccess is read from top to bottom. There are several ways to exclude subdomains from the redirect. Here's an example of another way to do it:Exclude subdomains. Is one way better than the other? It's typically a matter of opinion. Make sure to put your rules for the subdomain re-direct ABOVE your other redirect rule. The subdomain rule should be processed first. I hope this helps to answer your question, please let us know if you require any further assistance. Regards, Arnel C.