apache - htaccess If...Else always selects Else -
i set environment variable in httpd-vhosts.conf
setenv early_var 1
i try setting special rules based on value in .htaccess
<if "%{env:early_var} == '1'"> setenv test_var if_branch </if> <else> setenv test_var else_branch </else>
i expect test_var
environment var equal if_branch
. in php
var_dump(getenv('early_var')); // string '1' var_dump(getenv('test_var')); // string 'else_branch'
i tried setting early_var
in .htaccess
above if/else, both using setenv
, setenvif
. else branch executed.
why this?
apache 2.4
without using expression i.e. if/else
directives can this:
# set early_var 1 setenvif host ^ early_var=1 # if early_var 1 set test_var if_branch setenvif early_var ^1$ test_var=if_branch # if early_var not 1 set test_var else_branch setenvif early_var ^(?!1$) test_var=else_branch
this work older apache versions i.e. <2.4
edit:
in apache config or vhost config have variable initialization:
setenvif host ^ early_var=5
then in .htaccess can use:
<if "env('early_var') == '5'"> setenv test_var if_branch </if> <else> setenv test_var else_branch </else>
reason why need env variable declaration in apache/vhost config because .htaccess loaded after apache/vhost config , env variables set in same .htaccess not available evaluation in if/else
expressions.
also, note important use setenvif
instead of setenv
make these variables available if/else
expressions.
Comments
Post a Comment