Certificate must cover both www and non-www https. Some provider's certs cover both for www.xxxx.yyy, but only one for xxxx.yyy.
Turn on rewrites:
RewriteEngine On
Make all http use https:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://xxx.yyy/$1 [L,R=301]
Make only www https use the non-www https:
RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^www[.].+$
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [L,R=301]
Cannot be processing non-www https, otherwise a loop occurs.
In [L,R=301]:
- L = If the rule was processed, don't process any more.
- R=301 = Tells browser/robot to do a permanent redirect.
More generic
A more generic approach -- not port-dependant -- is:
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [R=301,QSA]
to make any url with www
drop it.
RewriteCond %{HTTPS} !on
RewriteCond %{HTTPS} !1
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [R=301,QSA]
to force any non-https url, even for those system downstream from load-balancers that drop https, use https.
Note that I have not tested the forwarded
options, so would appreciate feedback on any issues with them. Those lines could be left out if your system is not behind a load-balancer.
TO HTTP_HOST or not
You can use ${HTTP_HOST}
to be part of the URL in the RewriteRule
, or you can use your explicit canonical domain name text (xxxx.yyy
above).
Specifying the domain name explicitly ensures that no slight-of-hand character-bending means are used in the user-supplied URL to possibly trick your site into doing something it might not be prepared for, or at least ensures that the proper domain name appears in the address bar, regardless of which URL string opened the page.
It might even help convert punycode-encoded domains to show the proper unicode characters in the address bar.