mod_rewrite and user environment variables

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

    Setenv foo bar

    I needed to use such a custom environment variable in the RewriteCond directive of the mod_rewrite module. It turned out, however, that the variables set using SetEnv are not available in mod_rewrite :(. Alas!

    What to do? The solution is very simple. Using this simple rule, you can set an environment variable that mod_rewrite can then use:

    RewriteRule. * - [E = foo: bar]

    Redirection does not occur, we simply set the environment variable foo with the value bar ( A dash indicates that no substitution should be performed (the existing path is passed through untouched) - )

    Further 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 I understand it, it is required to use its environment variables, while standard ones like QUERY_STRING can be used without it)

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

    Also popular now: