.htaccess to require WWW for domain, but allow sub

2020-03-27 02:43发布

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

2条回答
趁早两清
2楼-- · 2020-03-27 03:31

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]
查看更多
干净又极端
3楼-- · 2020-03-27 03:35
RewriteCond %{HTTP_HOST} ^example\.org
RewriteRule ^ http://www.example.org%{REQUEST_URI} [L,R=301]

This will not redirect subdomains like mail.example.org

查看更多
登录 后发表回答