Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 515
Yarin Avatar asked Sep 16 '11 18:09

Yarin


People also ask

What is the domain root folder of a website?

The root directory is the folder that is accessed when internet users type the domain name of a website into the search bar of their browser. When a website with an index. html file in the root directory is called up, the index. html file is displayed in the browser.

Where is the root level of my domain?

Finding the Document Root A domain's document root, also known as the home folder, is the main folder that contains all of the files for either a domain or a subdomain. The document root for your main domain name is your public_html folder.


2 Answers

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

like image 60
Imad Moqaddem Avatar answered Sep 19 '22 00:09

Imad Moqaddem


Will $url and $dir always be pointing to the same place?

Yes

<?php 
$some_relative_path = "hello"; 
$server_url = $_SERVER["SERVER_NAME"]; 
$doc_root = $_SERVER["DOCUMENT_ROOT"]; 


echo $url = $server_url.'/'. $some_relative_path."<br />"; 
echo $dir = $doc_root.'/'. $some_relative_path;

Output:

sandbox.phpcode.eu/hello
/data/sandbox//hello
like image 32
genesis Avatar answered Sep 19 '22 00:09

genesis