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 ?
NC = nocase means regardless of the case of the incoming referrer traffic match with the given pattern. See Apache [NC|nocase] flag.
RewriteCond %{REQUEST_FILENAME} !-f. RewriteCond %{REQUEST_FILENAME} !-d. … means that if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist then procede to the rewrite rule below.
htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.
RewriteCond is an apache mod-rewite directive which is used for conditional URL rewriting on Apache server. We use RewriteCond along with RewriteRule%{HTTP_HOST} , %{HTTPS} etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With