'<If "-T env('HTTP_VAR_VAR')"> and <IfDefine HTTP_VAR_VAR> NOT working in Apache httpd-2.4.43-win64-VS16 + PHP 8

this is Windows 10 + PHP 8 + Apache httpd-2.4.43-win64-VS16 (WAMP 2022)

my file .htaccess work 100% fine, when I add the line

php_value auto_prepend_file '/path/file.php'

into my .htaccess file.php is loaded and all work fine, 0errors, 0 warnings.

Now I need use a conditional into my .htaccess, then I put:

SetEnv HTTP_VAR_VAR1 'hello world1'
SetEnv HTTP_VAR_VAR2 'hello world2'

and result is 0 errors, the code PHP

var_dump(getenv('HTTP_VAR_VAR1'));
var_dump(getenv('HTTP_VAR_VAR2'));

return

string(12) "hello world1"
string(12) "hello world2"

yeah!, my .htaccess work fine

now I need evaluate the vars into my .htaccess and I add:

<If "-T env('HTTP_VAR_VAR1')">
php_value auto_prepend_file '/path/file1.php'
</If>

<IfDefine HTTP_VAR_VAR2>
php_value auto_prepend_file '/path/file2.php'
</IfDefine>

and NEVER auto_prepend_file is loaded

why

<If "-T env('HTTP_VAR_VAR')"> and <IfDefine HTTP_VAR_VAR>

not work ?



Solution 1:[1]

from https://httpd.apache.org/docs/2.4/mod/mod_env.html

The internal environment variables set by this directive are set after most early request processing directives are run, such as access control and URI-to-filename mapping. If the environment variable you're setting is meant as input into this early phase of processing such as the RewriteRule directive, you should instead set the environment variable with SetEnvIf.

then I add the lines

SetEnvIf Request_URI ".*USE_auto_prepend_file.*" HTTP_VAR_VAR3

<If "-T env('HTTP_VAR_VAR3')">
php_value auto_prepend_file '/path/file1.php'
</If>

into my .htaccess and code PHP from http://127.0.0.1/ONLY_THIS_DIR_USE_auto_prepend_file/file.php say:

string(1) "1"

but never /path/file1.php is loaded

only using directlly

php_value auto_prepend_file '/path/file.php'

the file is included

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stackoverflow