You Old Fool
You Old Fool

Reputation: 22959

How does this Apache environment variable syntax work?

I've seen a bunch of examples for dynamically setting request origin headers in htaccess that all look approximately like this:

SetEnvIf Origin "^http(s)?://(.+\.)?(example\.com|example\.org)$" REQUEST_ORIGIN=$0
Header always set Access-Control-Allow-Origin %{REQUEST_ORIGIN}e env=REQUEST_ORIGIN

However, I can't seem to find an explanation for the syntax of that second line anywhere. I also looked here where I found an example with similar syntax but again, nothing to explain how it works:

https://httpd.apache.org/docs/2.4/env.html

Specifically, I want to know about %{REQUEST_ORIGIN}e and env=REQUEST_ORIGIN.

What does each of those things do? The former appears to be doing something with the variable but how does the brace syntax work and why the trailing e?

Upvotes: 2

Views: 2363

Answers (1)

WoLfulus
WoLfulus

Reputation: 1977

The second line sets the Access-Control-Allow-Origin only if REQUEST_ORIGIN variable is set.

About %{REQUEST_ORIGIN}e it tells that it needs to grab the value from environment variable.

Basically these two lines together checks for valid/secure origins and set CORS headers to their proper values if check passes.

Documentation

The Header directive may be followed by an additional argument, which may be any of:

...

env=[!]varname

The directive is applied if and only if the environment variable varname exists. A ! in front of varname reverses the test, so the directive applies only if varname is unset.

As for the e syntax:

%{VARNAME}e The contents of the environment variable VARNAME.

References:

http://httpd.apache.org/docs/current/mod/mod_headers.html

Upvotes: 3

Related Questions