htaccess If…Else always selects Else

前端 未结 4 1794
温柔的废话
温柔的废话 2021-02-12 21:59

I set an environment variable in httpd-vhosts.conf

SetEnv EARLY_VAR 1

I try setting special rules based on its value in

相关标签:
4条回答
  • 2021-02-12 22:31

    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)

    0 讨论(0)
  • 2021-02-12 22:41

    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>
    
    0 讨论(0)
  • 2021-02-12 22:45

    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.

    0 讨论(0)
  • 2021-02-12 22:49

    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.

    0 讨论(0)
提交回复
热议问题