Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Seems like this should be easy, but I cannot figure out the syntax. In Apache, I want to use the value of an existing request header to set a new request header. Some simple non-working code that illustrates what I'd like to do:

RequestHeader set X-Custom-Host-Header "%{HTTP_HOST}e"

Ideally, this would make a new HTTP header in the request called "X-Custom-Host-Header" that contains the value of the existing Host header. But it does not. Perhaps I need to copy the existing header into an environment variable first? (If so, I can't figure out how to do that either.)

I feel like I'm missing something obvious, but I've gone over the Apache docs and I can't figure it out. Thanks for any help.

share|improve this question

migrated from stackoverflow.com Dec 13 '09 at 17:55

This question came from our site for professional and enthusiast programmers.

add comment

1 Answer

The FOOBAR in %{FOOBAR}e should be an environment variable, but HTTP_HOST is a server variable.

If you really want to do that, you may try:

RewriteRule (.*) $1 [E=custom_host:%{HTTP_HOST}]
RequestHeader set X-Custom-Host-Header "%{custom_host}e"

or

RewriteCond %{HTTP_HOST} (.*)
RewriteRule (.*) $1 [E=custom_host:%1]
RequestHeader set X-Custom-Host-Header "%{custom_host}e"

or

SetEnvIf Host (.*) custom_host=$1
RequestHeader set X-Custom-Host-Header "%{custom_host}e"

All untested.

Not sure of the first one, but the second and third one should work.

share|improve this answer
    
I tried all of these, none of them worked. The header is not being set. –  Lauri Lehtinen Apr 26 '11 at 4:45
    
I tried the third form, and also set a response header. The response header was set properly. –  davetron5000 Apr 28 '11 at 19:31
1  
Third form worked for on getting the request header, too. –  davetron5000 Apr 29 '11 at 15:57
1  
The 3rd option also worked for me and the explanation makes sense - thanks –  stephen mulcahy Jun 8 '11 at 13:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.