6

This is an exension to this question where we learn that we can set a $_SERVER var using SetEnv.

The next question is: Is there a way to use SetEnv essentially 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
  • So you specifically want the directory the .htaccess file is in? Commented Jun 28, 2012 at 21:22
  • I'm asking two questions, really: (1) Is it possible to use variables when setting variables? (The answers below tell me, yes); and (2) How can I use that functionality to set an environment variable with the directory the .htaccess file is in? Commented Jun 28, 2012 at 21:26
  • OK. The question is, can a RewriteRule known what directory the .htaccess file is in. I don't think so... but you could use %{DOCUMENT_ROOT} plus some static string... I'll research this further... Commented Jun 28, 2012 at 21:27
  • 1
    Why would you need to know this? Commented Jun 28, 2012 at 21:33
  • After some research I am fairly certain that there's no way a .htaccess file can know the path to itself. So I think you'll need to use hardcoded paths... Commented Jun 28, 2012 at 21:37

3 Answers 3

6

Depending on the information you require, you may be able to do this with a RewriteRule. For example:

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

will set the PHP var $_SERVER['RESOURCE_ROOT'] or $_SERVER['REDIRECT_RESOURCE_ROOT'] to your Apache Document Root. See the mod_rewrite manual for full details, but some combination of %{DOCUMENT_ROOT}, %{PATH_INFO} and %{REQUEST_FILENAME} may work for you.

I don't think it's possible for the .htaccess file to know what directory it resides in. So I think you'll need to use hardcoded paths inside your .htaccess file. As an example:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT} (.*)
RewriteRule (.*) - [E=RESOURCE_ROOT:%1/module/unique_section/]
Sign up to request clarification or add additional context in comments.

Comments

4

as far as I can tell, setEnv doesn't have access to the apache environment variables. So the %{DIRECTORY} wouldn't work unless you wanted the value to be specifically "%{DIRECTORY}". Using mod_rewrite, this worked for me to get the path of the file being loaded:

RewriteEngine On

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

outputs:

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

Other than that, is there anything wrong with just using php and doing dirname(__FILE__) or dirname($_SERVER['PHP_SELF'])?

2 Comments

THANKS! This is VERY close to what I'm asking, but I'm not asking for the directory that the script is in, I'm asking for the directory that the .htaccess is in. (Look at the paths and output in the question a little closer to see what I mean.)
@NathanJ.Brauer AFAIK you can't find out which .htaccess file is being loaded without using php and just going up the path until you find an .htaccess file.
2

You can use the function apache_getenv to get what you want.

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

Do not forget to read the documentation :

This function requires Apache 2 otherwise it's undefined.

Example :

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

The above example will output something similar to:

42.24.42.240

But, as suggested in this threat, you could just use dirname(__FILE__) and since PHP 5.3.0 you can use __DIR__ as a magic constant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.