Can Apache conditionally perform a rewrite from a custom http header?

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


Solution 1:

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]