Set header in apache if it doesn't already exist

Solution 1:

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.

Solution 2:

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

Solution 3:

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 "^$" "*"