How do I get the root url of the site?

How do I get the root url of the site?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting the Root URL in PHP: Beyond DOCUMENT_ROOT

Distinguishing between a directory path and a fully qualified domain name (FQDN) is a common source of confusion when handling URLs in PHP. Many developers instinctively reach for $_SERVER['DOCUMENT_ROOT'], but as you've likely encountered, this variable provides the filesystem path where the web server's root is mounted, not the URL itself. If you are trying to strip a query string or a sub-path from a full request URL to isolate the base domain and protocol, relying on filesystem variables will lead to mismatches between your application logic and the actual browser address bar.

The most reliable approach for extracting the root URL lies within the $_SERVER superglobal array, specifically utilizing $_SERVER['REQUEST_SCHEME'], $_SERVER['HTTP_HOST'], and $_SERVER['PHP_SELF']. However, a more idiomatic solution in modern PHP development is to leverage the $_SERVER['DOCUMENT_URI'] or, even better, the $_SERVER['REQUEST_URI'] combined with protocol detection.

To handle the specific case of converting http://localhost/some/folder/containing/something/here/or/there into http://localhost/, you need to reconstruct the base URL programmatically. A robust pattern involves checking if a scheme is present; if not, defaulting to https. You then concatenate the scheme with the host and an empty path component.

$protocol = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http');
$host = $_SERVER['HTTP_HOST'];

// This reconstructs the root URL without any path or query parameters
$rootUrl = $protocol . '://' . $host;

This method is generally superior to parsing $_SERVER['PHP_SELF'] because it avoids potential issues with how different web servers handle trailing slashes or virtual host configurations. It directly targets the components that define the origin of the request.

Handling Subdomains and Virtual Hosts

Your concern regarding subdomains, such as transforming a path on http://subsite.localhost to the root http://subsite.localhost/, is addressed by this approach. The HTTP_HOST variable captures the entire hostname including any subdomain prefix provided by the client. By simply omitting any path segments in your reconstruction logic, you ensure that the resulting URL always points to the domain root, regardless of the depth of the current directory structure.

This technique works consistently across various environments:

  1. Local Development: Whether running on XAMPP, MAMP, or a native Apache/Nginx setup with localhost or subsite.localhost.
  2. Production: The logic remains valid whether the host is example.com, api.example.com, or a custom CNAME.

Trade-offs and Security Considerations

While this solution is straightforward, there are specific nuances to consider when deploying in production environments. One common pitfall involves the handling of the trailing slash. In many frameworks, including Laravel, the default behavior often appends a trailing slash to the root URL for consistency. If your application logic expects a trailing slash (e.g., for base path replacement), you may need to normalize the result:

$rootUrl .= '/'; 
// Result: http://subsite.localhost/

Another critical consideration is the distinction between the host and the domain name in browser history or bookmarks. Some legacy systems might return HTTP_HOST without a scheme if the request was made via an absolute URL, though strictly following RFC standards usually guarantees the presence of the scheme in REQUEST_SCHEME. If that variable is missing, deriving the protocol from the HTTPS environment variable is the standard fallback.

For frameworks like Laravel, you would typically use the helper function url('/'), which abstracts this underlying logic and handles the trailing slash conventions automatically. However, understanding the raw mechanism ensures you can solve similar problems in vanilla PHP or when debugging server-level configuration issues.

If your architecture involves complex routing where the root path itself is dynamic (e.g., a subdirectory installation like http://localhost/myapp/), simply stripping the current path might not suffice. In those cases, checking for a configured base URL in your environment variables becomes necessary to avoid hardcoding assumptions about the installation directory.

Our network