RewriteCond %{HTTP_HOST} !^mysite.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
This strips www from the URL.
Without the rule the subdomain works (blog.site.com). With the rule it converts back to (site.com/blog) and shows 404 errors.
What do I need to do to keep this from affecting the subdomain(s)?
Also, I'm curious if removing the www. is a good thing? I've heard it doesn't matter as long as you pick one and stick to it. I'm also using <link rel="canonical" ... />
to re-enforce it.
With (blog\.)?
at the begin blog. becomes optional and so it will redirect only if it is different from blog.mysite.com
and mysite.com
.
RewriteCond %{HTTP_HOST} !^(blog\.)?mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
A different way to do the same would be:
RewriteCond %{HTTP_HOST} !^mysite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^blog\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
Which means if hostname is different then mysite.com
and blog.mysite.com
redirect.
To avoid duplicity it is good to choose between www or non-www and redirect every thing for that.
So if u use www then redirect non-www to www and vice versa.