Setting DYNAMIC Php $ _SERVER ($ _SERVER ['something']) using Apache.htaccess

This is a question of this question , where we find out that we can install $_SERVERvar with SetEnv.

Next question: is there a way to use SetEnv basically like this:

/var/www/www.example.com/module/unique_section/.htaccess:

SetEnv RESOURCE_ROOT %{DIRECTORY}

/var/www/www.example.com/module/unique_section/some/path/file.php

<?php echo $_SERVER['RESOURCE_ROOT']; ?>

Output: /var/www/www.example.com/module/unique_section/

+5
source share
3 answers

Depending on the information you need, you can do this with RewriteRule. For instance:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT} (.*)
RewriteRule (.*) - [E=RESOURCE_ROOT:%1]

PHP var $_SERVER['RESOURCE_ROOT'] $_SERVER['REDIRECT_RESOURCE_ROOT'] Apache Document Root. . mod_rewrite, %{DOCUMENT_ROOT}, %{PATH_INFO} %{REQUEST_FILENAME}.

, .htaccess , . , .htaccess. :

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT} (.*)
RewriteRule (.*) - [E=RESOURCE_ROOT:%1/module/unique_section/]
+5

, setEnv apache. ,% {DIRECTORY} , , "% {DIRECTORY}". mod_rewrite, :

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} ((.*)/)
RewriteRule (.*) - [E=RESOURCE_ROOT:%1]

:

var_dump(__FILE__);
//outputs: string(27) "/var/www/sub/test/index.php" 
var_dump($_SERVER['RESOURCE_ROOT']);
//outputs: string(24) "/var/www/sub/test/"

, - , php dirname(__FILE__) dirname($_SERVER['PHP_SELF'])?

+4

You can use the function apache_getenvto get what you want.

Link: http://php.net/manual/en/function.apache-getenv.php

Remember to read the documentation:

This function requires Apache 2 , otherwise it is undefined.

Example:

<?php
$ret = apache_getenv("SERVER_ADDR");
echo $ret;
?>

In the above example, something similar to:

42.24.42.240

But, as suggested in this threat, you can just use it dirname(__FILE__), and starting with PHP 5.3.0 you can use it __DIR__as a magic constant.

+2
source

All Articles