htaccess If…Else always selects Else

匆匆过客 提交于 2019-12-04 00:36:39

Without using expression i.e. if/else directives you can do this:

# set EARLY_VAR to 1
SetEnvIf Host ^ EARLY_VAR=1

# if EARLY_VAR is 1 then set TEST_VAR to if_branch
SetEnvIf EARLY_VAR ^1$ TEST_VAR=if_branch

# if EARLY_VAR is NOT 1 then set TEST_VAR to else_branch
SetEnvIf EARLY_VAR ^(?!1$) TEST_VAR=else_branch

This will work with even older Apache versions i.e. <2.4


EDIT:

In your Apache config or vhost config have this variable initialization:

SetEnvIf Host ^ EARLY_VAR=5

Then in your .htaccess you can use:

<If "env('EARLY_VAR') == '5'">
   SetEnv TEST_VAR if_branch
</If>
<Else>
   SetEnv TEST_VAR else_branch
</Else>

Reason why you need env variable declaration in Apache/vhost config because .htaccess is loaded after Apache/vhost config and env variables set in same .htaccess are not available for evaluation in if/else expressions.

Also, note that it is important to use SetEnvIf instead of SetEnv to make these variables available to if/else expressions.

Looking at the documentation of SetEnv directive, I notice:

Sets an internal environment variable, which is then available to Apache HTTP Server modules, and passed on to CGI scripts and SSI pages.

which means that the environments are then available to other modules. Whereas, <if> directive is provided by the core. This can be confirmed if you try to see whether the environment is empty during the <if> clause:

<If "-z %{ENV:EARLY_VAR}">
   SetEnv TEST_VAR  if_branch
</If>

or

<If "-z env('EARLY_VAR')">
   SetEnv TEST_VAR  if_branch
</If>

both will give you:

string(1) "1" string(9) "if_branch" 

in your PHP.


There is also the following caveat listed on the Apache's environment variables wiki/docs pages:

The SetEnv directive runs late during request processing meaning that directives such as SetEnvIf and RewriteCond will not see the variables set with it.

The only way I could make it work is using SetEnvIf both in .conf and .htaccess:

I used only this in .conf

SetEnvIf Server_Addr ^ EARLY_VAR=1

and then only this in .htaccess :

SetEnvIf EARLY_VAR ^ TEST_VAR=else_branch
SetEnvIf EARLY_VAR ^1$ TEST_VAR=if_branch

(without If directive. Whatever I tried within If, it seemed like it was evaluated before EARLY_VAR is set)

If you found problem with SetEnv and SetEnvIf in your Apache configuration.

Set the variable in httpd.conf file as below

SetEnvIf Host ".*" EARLY_VAR=1
SetEnvIf Host eval EARLY_VAR=1
SetEnvIf Host dev EARLY_VAR=1

It assign the value in all type of server (production ,evaluation or development)

Now you can use your variable in .htaccess file.

<IF "%{ENV:EARLY_VAR} == 1">
    SetEnv TEST_VAR if_value
</IF>
<ELSE>
    SetEnv TEST_VAR else_value
</ELSE>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!