7

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 ?

hsz
  • 148,279
  • 62
  • 259
  • 315

1 Answers1

17

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:

SetEnvIf APPLICATION_ENV  ^(.*)$ APPLICATION_ENV=development

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).

EDIT
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.
Qben
  • 2,617
  • 2
  • 24
  • 36
  • 1
    You're right, the explanation is in [Apache documentation](http://httpd.apache.org/docs/2.2/en/mod/mod_env.html#setenv): 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. – julp Oct 14 '13 at 15:19
  • I've tested this issue on apache2, no better result as the OP. – jacouh Oct 14 '13 at 15:31
  • @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. – Qben Oct 14 '13 at 16:52
  • @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 ! :) – hsz Oct 15 '13 at 07:03