Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess rewriteCond using custom http header

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?

like image 322
Wintermute Avatar asked Dec 12 '25 17:12

Wintermute


2 Answers

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]
like image 84
user1018130 Avatar answered Dec 15 '25 22:12

user1018130


you set "IS-HTTPS" and you check for "IS_HTTPS" ?

like image 31
Gerard H. Pille Avatar answered Dec 15 '25 23:12

Gerard H. Pille