12

I have an apache server behind a (simple amazon) load balancer. I want to redirect any incoming traffic that is not 443 to 443. I would prefer it to use just one apache virtual host. So I'm trying to detect that if the HTTP_X_FORWARDED_PORT header is not 443.

I've checked the RewriteCond docs and it only works with a limited set of HTTP headers.

Basically what I'm doing is this:

<VirtualHost *:80>
        ServerName www.example.com

        RewriteEngine On
        RewriteCond %{HTTP_X_FORWARDED_PORT} !=443
        RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

        ....
</VirtualHost>

But RewriteCond doesn't recognize HTTP_X_FORWARDED_PORT.

Is there any other way to accomplish this (with just one VirtualHost)? (some type of incoming header check?)

Thanks, Lance

Lance Rushing
  • 255
  • 2
  • 3
  • 6

1 Answers1

14

A little further down the RewriteCond document:

%{HTTP:header}, where header can be any HTTP MIME-header name

So, you can do it, just not in quite the same form as the pre-extracted headers.

Try this:

RewriteCond %{HTTP:X-Forwarded-Port} !=443
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Shane Madden
  • 114,520
  • 13
  • 181
  • 251
  • 1
    Another example with an IP address in a header passed by SiteLock CDN: `RewriteCond %{HTTP:X-Forwarded-For} !=123.45.67.89` – Liam Aug 26 '15 at 16:22