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:
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.
hsz
IntelliJ SDK, Kotlin, Java, TypeScript, NodeJS, security, ...
Updated on June 27, 2022Comments
-
hsz 7 monthsIn my
.htaccessI set:SetEnv APPLICATION_ENV developmentAnd I want to check if
APPLICATION_ENVequalsdevelopmentthen runapp_dev.php, otherwiseapp.phpSetEnv 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.phpscript. How should I fix it ? -
julp over 9 yearsYou'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 over 9 yearsI've tested this issue on apache2, no better result as the OP. -
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 over 9 years@Qben Thank you for answering. I've setSetEnvIfin myVirtualConfdefinition, so.htaccessfile can be pushed to the production, now I'm free of any hacks. Thanks ! :)