Paco

Personal Content Organiser

Home / Linux / apache

Navigation

Menu

Search

.htaccess rewrite all to https

These rules are intended to be used in .htaccess files, as a RewriteRule in a *:80 VirtualHost entry needs no Conditions.
RewriteEngine on RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]


Eplanations:
RewriteEngine on


==> enable the engine at all
RewriteCond %{HTTPS} off [OR]


==> match on non-https connections, or (not setting [OR] would cause an implicit AND !)

RewriteCond %{HTTP:X-Forwarded-Proto} !https


==> match on forwarded connections (proxy, loadbalancer, etc.) without https
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

==> if one of both Conditions match, do the rewrite of the whole URL, sending a 301 to have this 'learned' by the client (some do, some don't) and the L for the last rule.



nginx



server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 301 https://$host$request_uri; }