Wintermute
Wintermute

Reputation: 3045

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?

Upvotes: 3

Views: 13917

Answers (2)

Gerard H. Pille
Gerard H. Pille

Reputation: 2578

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

Upvotes: 2

user1018130
user1018130

Reputation: 195

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]

Upvotes: 4

Related Questions