Paths and URLs

by Andy Prevost

Saturday January 10 2026

Open Source software, that is software a developer offers to other developers at no charge, can lead to some strange issues.

Two of my products, ttCMS and DCP-Portal, struggled with two majors issues. Dealing with installations in subdomains, and dealing with multi-site installations. The second was fairly easy to deal with. I implemented a database table prefix that could be changed based on the site.

The first, not so easy to deal with. 

I came up with several routines to get the name of the subdomain ... none were really the right way (although there was some success). 

It's been a few years now, and I've come up with an algorithm that works. 

From the installation directory (the main directory of the subdomain), call two global variables:

  • __DIR__ 
  • $_SERVER['DOCUMENT_ROOT']

The first "__DIR__" gets you the entire path, including the subdomain. 

THe second "$_SERVER['DOCUMENT_ROOT']" gets you the entire path – without the subdomain. The path stops at the root level of the top level domain.

To get the subdomain path by itself, do a str_replace that removes the $_SERVER['DOCUMENT_ROOT'] from __DIR__ and all you are left with is the subdomain.

Here's what it looks like:

$app_full_path = __DIR__;
$app_path = ltrim(str_replace($_SERVER['DOCUMENT_ROOT'], '', $app_full_path), '/');

Note the ltrim in the last line, that removes any leading forward slashes. 

Works like a charm ....

 

◀ Previous Next ▶

Post a Comment