.htaccess if ENV is true or specified

I need to run a set of RewriteCond statements if ENV is true or certain (if not defined and then considered false). I tried to use:

RewriteCond %{ENV:IS_MOBILE} ^true$ RewriteCond ... 

and (not sure if this is even true)

 RewriteCond %{ENV:IS_MOBILE} !=true RewriteCond... 

But it doesn't seem to work. How can i do this?

+7
source share
2 answers

Here is a real life example to help you find a solution:

If the host starts with "s" or "static", then create the environment variable "STATIC" and set it to "1":

 RewriteCond %{HTTP_HOST} (s|static)\.mysite\.com$ RewriteRule (.*) - [QSA,E=STATIC:1] 

Then you can search: if my hosts are "static", then if the files are considered "static" (img, etc.), then stop:

 RewriteCond %{ENV:STATIC} 1 RewriteRule (.*)\.(pdf|jpg|jpeg|gif|png|ico)$ - [L] 

Now, if you want to be sure that a variable does not exist, just check to see if it is empty. Here is my rule immediately after the previous one: it checks to see if the variable is not empty, and if so, then error = 404 (to simulate "not found"):

 RewriteCond %{ENV:STATIC} !^$ RewriteRule (.*) /404.php [L] 

In your case, if your env is not empty, you are assuming this is true:

 RewriteCond %{ENV:IS_MOBILE} !^$ RewriteCond... 

In your case, if your env is exactly matches the string " true ", then you are assuming that it is "true":

 RewriteCond %{ENV:IS_MOBILE} true RewriteCond... 

Hope this helps.

+11
source

I think you need to wrap the code first with IfDefine

 <IfDefine %{ENV:IS_MOBILE}> RewriteCond %{ENV:IS_MOBILE} true </IfDefine> 
0
source

Source: https://habr.com/ru/post/901422/


All Articles