.htaccess to require WWW for domain, but allow subdomain if existing with no hard coding

核能气质少年 提交于 2019-12-01 08:25:31

问题


I'm trying to figure it out how to set up an .htaccess set of rules that would force the presence of the "www" in front of the domain if it is not initially specified, but at the same time, it will not have any effect if the an subdomain is pressent; all this without hard coding any domain name so that the script is portable around different servers and configurations.

EDIT:

I'm sorry I was not able to get this explained right in the fist place. So what I need is as follows:

http://example.com -> redirects to http://www.example.com

http://www.example.com -> does not redirect

http://subdomain.example.com -> does not redirect


回答1:


This mod_rewrite rule should do it for second level domains:

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]



回答2:


RewriteCond %{HTTP_HOST} ^example\.org
RewriteRule ^ http://www.example.org%{REQUEST_URI} [L,R=301]

This will not redirect subdomains like mail.example.org



来源:https://stackoverflow.com/questions/1263170/htaccess-to-require-www-for-domain-but-allow-subdomain-if-existing-with-no-har

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