Search code examples
phppathdocument-rootbase-url

Determining a website's 'root' location


I need some help with concepts and terminology regarding website 'root' urls and directories.

Is it possible to determine a website's root, or is that an arbitrary idea, and only the actual server's root can be established?

Let's say I'm writing a PHP plugin that that will be used by different websites in different locations, but needs to determine what the website's base directory is. Using PHP, I will always be able to determine the DOCUMENT_ROOT and SERVER_NAME, that is, the absolute URL and absolute directory path of the server (or virtual server). But I can't know if the website itself is 'installed' at the root directory or in a sub directory. If the website was in a subdirectory, I would need the user to explicitly set an "sub-path" variable. Correct?


Solution

  • Answer to question 1: Yes you need a variable which explicitly sets the root path of the website. It can be done with an htaccess file at the root of each website containing the following line :

    SetEnv APP_ROOT_PATH /path/to/app
    

    http://httpd.apache.org/docs/2.0/mod/mod_env.html

    And you can access it anywhere in your php script by using :

    <?php $appRootPath = getenv('APP_ROOT_PATH'); ?>
    

    http://php.net/manual/en/function.getenv.php