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

六眼飞鱼酱① 提交于 2019-12-20 10:47:03

问题


My primary domain is currently permanently redirected to www.mydomain.com (non-www to www redirection), 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 subdomains that I'll be creating, ex. blog.mydomain.com, will be redirected to non-www, ex. blog.mydomain.com, and not www.blog.mydomain.com. Every time I create a subdomain and enter the non-www URL to the browser, it prompts a redirect loop.

Hope you can help! Thanks! :)


回答1:


Keep this one rule for all the sub-domains:

# 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]



回答2:


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]


来源:https://stackoverflow.com/questions/21985138/how-to-redirect-www-subdomain-to-non-www-when-domain-is-redirected-to-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!