RewriteCond with SetEnv


Since I had to test this myself and could only confirm that what you wrote was correct, I started to look around and found this post regarding SetEnv, SetEnvIf and RewriteRule visibility. It looks like SetEnv is not visible for RewriteCond and if I change your example to use:


I actually get the rules you have to load app_dev.php. You could set the variable using RewriteRule as well:

RewriteRule .* - [E=APPLICATION_ENV:development,NE]

However, looks like SetEnv can't be used (Tested on Apache 2.2.22).

As julp pointed out in a comment to this post this is quite clear in Apache document section Setting Environment Variables:

The SetEnv directive runs late during request processing meaning that directives 
such as SetEnvIf and RewriteCond will not see the variables set with it.
Author by


IntelliJ SDK, Kotlin, Java, TypeScript, NodeJS, security, ...

Updated on June 27, 2022


  • hsz
    hsz 7 months

    In my .htaccess I set:

    SetEnv APPLICATION_ENV development

    And I want to check if APPLICATION_ENV equals development then run app_dev.php, otherwise app.php

    SetEnv APPLICATION_ENV development
    RewriteCond %{ENV:APPLICATION_ENV} development
    RewriteRule .? %{ENV:BASE}/app_dev.php [L]
    RewriteRule .? %{ENV:BASE}/app.php [L]

    However it does not work - always runs app.php script. How should I fix it ?

  • julp
    julp over 9 years
    You're right, the explanation is in Apache documentation: The internal environment variables set by this directive [SetEnv] are set after most early request processing directives are run, such as access control and URI-to-filename mapping. If the environment variable you're setting is meant as input into this early phase of processing such as the RewriteRule directive, you should instead set the environment variable with SetEnvIf.
  • jacouh
    jacouh over 9 years
    I've tested this issue on apache2, no better result as the OP.
  • Qben
    Qben over 9 years
    @julp Ahh, thanks I don't understand how I could have missed it when reading about it. Good to have it explained in the official docs.
  • hsz
    hsz over 9 years
    @Qben Thank you for answering. I've set SetEnvIf in my VirtualConf definition, so .htaccess file can be pushed to the production, now I'm free of any hacks. Thanks ! :)