Forcing users to access the site with www. and https
I am trying to write an .htaccess rule that appends www. to the domain and s to http if required but I can't seem to find a rule or set of rules that works for each case.
The cases are...
\https://www.site.com - should just work
\http://www.site.com - should go to \https://w开发者_如何学Pythonww.site.com
\http://site.com - should go to \https://www.site.com
\https://site.com - - should go to \https://www.site.com
Any help would greatly appreciated.
Try this rule:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://www.%2%{REQUEST_URI} [L,R=301]
This should cover both cases in which either the request was not HTTPS (first condition) or the host does not start with www.
(second condition). In that case the third condition will grab the host without the starting www.
(if present) that is then used in the rule’s substitution.
精彩评论