Toby
Toby

Reputation: 8792

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://www.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.

Upvotes: 0

Views: 156

Answers (1)

Gumbo
Gumbo

Reputation: 655489

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.

Upvotes: 2

Related Questions