Setting Apache request headers with environment variable values.

This is an Apache tip for picking up environment variables and placing them into the request headers, presumably so that a web application server can have access to information it would not otherwise have.

It seems like the following would be a perfectly good way to snatch the BAR environment variable (assuming such a variable existed) and pass it along. It doesn't work. I'm not sure why exactly. It might be due to the order of execution, or that FOO is masked from the headers module, or that BAR is a special Apache environment variable, rather than an environment variable that existed when the processed was started.

RewriteRule .* - [E=FOO:%{BAR}]RequestHeader set X-Foo "%{FOO}e"

Just to be safe, I tried this more verbose method:

RewriteEngine onRewriteRule .* - [E=FOO:%{BAR}]PassEnv FOORequestHeader set X-Foo "%{FOO}e"

That doesn't work either, and it drives me crazy not knowing why it fails. Regardless, after banging on the problem for a while, I came up with a solution that works like a charm:

SetEnvIfNoCase BAR "(.*)" FOO=$1RequestHeader set X-Foo "%{FOO}e"

My use case is to grab the SERVER_ADDR (IP address that actually recieved the HTTP request) and pass it along to JRun/ColdFusion. For whatever reason, that value doesn't exist in the data that mod_jrun22 passes along to the servlet. This is also a nice way to pass the exposed public IP address of a proxy server, rather than parsing the forwarding headers.