HTACCESS help please re: subdomains and redirects

guerilla

All we do is win
Aug 18, 2007
11,424
428
0
No
I have a site that only runs content on the www. root.

like http://www.domain.com

This is the htaccess code

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

I am using to redirect all

http://domain.com

to

http://www.domain.com

But now I want to add

http://blogs.domain.com
http://community.domain.com

etc

Should I just remove this htaccess code, is there a way to rewrite it so it only redirects

http://domain.com

to

http://www.domain.com

instead of redirecting all

http://*.domain.com

to

http://www.domain.com?



TIA, +rep for quality help.
 


Something like this
Code:
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(www|subdomain1|subdomain2|subdomain3)\.domain\.com
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

First block to see if its simply the domain with no subdomain or www.

Second to see:
1) If it has a subdomain
2) but the subdomain is not www, subdomain1, and so on
3) then default to www.
 
  • Like
Reactions: guerilla
If that works, I will take back everything bad I ever said about you! ;)

Couldn't I just use the first block though?

That's all I really want to do. Only redirect

domain.com

to

www.domain.com