Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RewriteCond with SetEnv

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 ?

like image 587
hsz Avatar asked Oct 14 '13 14:10

hsz


People also ask

What is NC in RewriteCond?

NC = nocase means regardless of the case of the incoming referrer traffic match with the given pattern. See Apache [NC|nocase] flag.

What is RewriteCond %{ Request_filename?

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.

What is RewriteCond in htaccess?

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

What is RewriteCond in Apache?

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.


1 Answers

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.
like image 187
Qben Avatar answered Sep 22 '22 10:09

Qben