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 almost 2 years
In my
.htaccess
I set:SetEnv APPLICATION_ENV development
And I want to check if
APPLICATION_ENV
equalsdevelopment
then runapp_dev.php
, otherwiseapp.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 over 10 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 10 yearsI've tested this issue on apache2, no better result as the OP.
-
Qben over 10 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 10 years@Qben Thank you for answering. I've set
SetEnvIf
in myVirtualConf
definition, so.htaccess
file can be pushed to the production, now I'm free of any hacks. Thanks ! :)