16

I have a proxy that is injecting some headers but I want to modify it so it only sets the headers if they are not already present:

<Location /api>    
    RequestHeader set MY_HEADER "value"

    ProxyPass http://127.0.0.1:8000/api
    ProxypassReverse http://127.0.0.1:8000/api
</Location>

Is this possible?

9

In Apache 2.4.7, x86_64, Ubuntu 14.04 LTS

I have found that this works

RequestHeader setIfEmpty X-Forwarded-For "127.0.0.1"

works all day long. However if one tries to use a dynamic value,

RequestHeader setIfEmpty X-Forwarded-For "%{REMOTE_ADDR}e"

does not work. I have found that you need the help of mod_rewrite to harvest the value. My configuration now looks like this, and it works.

RewriteRule . - [E=noxff:%{REMOTE_ADDR}] RequestHeader setIfEmpty X-Forwarded-For "%{noxff}e"

I know it is stoopid, but it works.

8

How about something like:

RequestHeader set X-My-Header "value" expr="req('X-My-Header')==''"

Consult http://httpd.apache.org/docs/current/expr.html for more info about the expr= syntax.

Edit: In Apache 2.2 you should be able to do this:

SetEnvIf X-My-Header "" no_my_header
RequestHeader set X-My-Header "value" env=no_my_header
4
  • I seem to be having some problems with that in apache 2.2.24. Is it just a 2.4 thing? – Hobozilla Jul 3 '13 at 15:16
  • Doesn't appear to exist in Apache 2.2. – Michael Hampton Jul 3 '13 at 17:31
  • See my edits above for Apache 2.2. – MLu Jul 3 '13 at 22:13
  • 2
    For some reason it doesn't like "" as a regular expression but ^$ seemed to do the trick. – Hobozilla Jul 4 '13 at 7:53
5

Apache 2.4.7

Header setifempty Access-Control-Allow-Origin "*"

Apache 2.2.4 below

Header append Access-Control-Allow-Origin ""
Header edit Access-Control-Allow-Origin "^$" "*"
1
  • The solution for Apache 2.2.4 and below listed here Header append Access-Control-Allow-Origin "" Header edit Access-Control-Allow-Origin "^$" "*" may have a side effect of appending , to a non-empty header value because it's how append action works. It's the case, at least, in Apache 2.4.6. – Artem Shafranov Feb 8 at 17:00
1

The following should be an equivalent to RequestHeader setIfEmpty in Apache versions <=2.4.6 (where setIfEmpty action is not supported):

RequestHeader set MY_HEADER "value" "expr=-z %{req:MY_HEADER}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.