PHP Document Root, Path and URL detection

less than 1 minute read

PHP has no built-in base URL variable. In all the mess that is the $_SERVER variable, there is nothing that will tell you the base URL for your website.  This is very useful when embedding assets and building URLs.

The thing is you can start from the DOCUMENT_ROOT and work your way from there, but if you are using Apache’s VirtualDocumentRoot, it is not reliable. Hence, you need to guess it.

The trick resides in SCRIPT_NAME and SCRIPT_FILENAME that respectively describe the executed PHP file, starting from the domain, and its full path. I used exclamation marks as delimiters as I doubt you have some in your folder names. If you do, then… shame on you.

While I was at it, I added some port and https detection and the absolute URL.

Example
Variable Content
base_dir /var/www/mywebsite
doc_root /var/www
base_url /mywebsite
protocol http
port 8080
domain example.com
full_url http://example.com:8080/mywebsite


Not included

  1. Port detection of a server running behind a proxy. You may want to use the port of the proxy, not the Web server.
  2. Same goes for https.
  3. Non-resolved symlinks and relative paths. You may want to throw a couple of realpath() in there.

Tags: ,

Updated: