adrien54 adrien54 - 23 days ago 5
Apache Configuration Question

Access in .htaccess an environnment variable defined in VirtualHost

I have defined in the apache configuration file an environnment variable in the vhost:

<VirtualHost *:443>
#...
SetEnv MY_VAR 123


I can access it in my application in PHP:

echo $_SERVER['MY_VAR'] //display 123


However I can't find a solution to access it in my
.htaccess
in a
RewriteCond
.

I tried:

RewriteCond %{MY_VAR} "123"


and

RewriteCond %{ENV:MY_VAR} "123"


Is this possible ? Thanks for your help.

Answer

You can use SetEnvIf module for this instead of SetEnv:

SetEnvIf Host ^ MY_VAR=123

This will set MY_VAR as 123 for any web request.

Then you can use this RewriteCond in your .htaccess:

RewriteCond %{ENV:MY_VAR} =123

Or using regex:

RewriteCond %{ENV:MY_VAR} ^123$