📜 ⬆️ ⬇️

mod_rewrite and user environment variables

There is a mod_env module in the Apache server, with its help you can set custom environment variables:

  SetEnv foo bar 

I needed to use such a custom environment variable in the mod_rewrite module's RewriteCond directive. It turned out, however, that the variables defined using SetEnv are inaccessible in mod_rewrite :(. Alas!

What to do? The solution is very simple. With the help of such a simple rule, you can set an environment variable, which mod_rewrite can then use:
')
  RewriteRule. * - [E = foo: bar] 

There is no redirection, we just set the foo environment variable with the value bar ( A dash should be performed) ).

Then this variable can be used:

  RewriteCond% {ENV: foo} bar # The following rule will be applied only if the variable foo has the value bar. 

(pay attention to the ENV prefix: as far as I understand, it is mandatory to use its environment variables, while the standard ones, such as QUERY_STRING, can be used without it)

That's all. I hope those who (like me) do not often have to configure Apache, this information will be useful

Source: https://habr.com/ru/post/103328/


All Articles