how to redirect www subdomain to non-www when domain is redirected to www how to redirect www subdomain to non-www when domain is redirected to www apache apache

how to redirect www subdomain to non-www when domain is redirected to www


Keep this one rule for all the sub-domains:

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


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]