htaccess rewriteCond using custom http header
Solution 1
I had a similar problem when nginx that was listening on both http and https ports was forwarding the traffic to a local apache instance.
In the nginx configuration i added:
proxy_set_header X-Request-Protocol $scheme; #http or https
In the .htaccess file i added this:
RewriteEngine On
RewriteCond %{HTTP:X-Request-Protocol} ^http$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
Solution 2
you set "IS-HTTPS" and you check for "IS_HTTPS" ?
Wintermute
Updated on June 04, 2022Comments
-
Wintermute almost 2 years
There are a thousand threads on this but I must be missing something as I can't get it to work.
My nginx load balancer decrypts SSL traffic and proxies it (via Varnish) through to the content servers. It adds a custom header to the proxied request:
proxy_set_header "IS-HTTPS" "1";
I can SEE this HTTP header from the content servers:
<?php var_dump($_SERVER["HTTP_IS_HTTPS"]); ?>
This will output string(1) "1" on a HTTPS connection, and NULL on a HTTP.
So, my .htaccess rules:
RewriteCond %{HTTP:IS_HTTPS} !="1" RewriteRule ^(securebit.*)$ https:// %{HTTP_HOST}/$1 [R=301,L]
Doesn't work. Just gets into a redirect loop.
(NB: the space in "// %" isn't there. StackOverflow validation is falling over on it.)
Neither do:
RewriteCond %{HTTP:IS_HTTPS} !=1
RewriteCond %{HTTP:IS_HTTPS} !1
RewriteCond %{HTTP:HTTP_IS_HTTPS} !="1"
RewriteCond %{HTTP:HTTP_IS_HTTPS} !=1
RewriteCond %{HTTP:HTTP_IS_HTTPS} !1
What simple, obvious and frustrating mistake am I making?
-
Jordan over 6 yearsNote that this doesn't redirect with a querystring
-
Gabor Garami almost 3 yearsYou could append QSA after L at the last line.