How to redirect www non-www subdomain when a domain is redirected to www

My primary domain is currently constantly being redirected to www.mydomain.com (redirecting without www to www), with .htaccess as follows:

RewriteCond %{HTTP_HOST} ^mydomain.com$ RewriteRule ^/?$ "http\:\/\/www\.mydomain\.com\/" [R=301,L] RewriteCond %{HTTP_REFERER} !^http://mydomain.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://mydomain.com$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.mydomain.com$ [NC] 

I would like to know how all the subdomains that I will create, for example. blog.mydomain.com will be redirected to non-www, for example. blog.mydomain.com , not www.blog.mydomain.com . Each time I create a subdomain and enter the non-www URL into the browser, it requests a redirect cycle.

Hope you can help! Thank you :)

+6
source share
2 answers

Keep this one rule for all subdomains:

 # rule for forcing www on main domain RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC] RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] # rule for removing www on sub domains RewriteCond %{HTTP_HOST} ^www\.([^.]+\.mydomain\.com)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] 
+9
source

This one supports http + https in one line:

 # Redirect www subdomain to non-www RewriteCond %{HTTP_HOST} ^www\.([^.]+\.yourdomain\.com)$ [NC] RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [R=301,L] 
0
source

All Articles