Get the full URL in PHP

Get the full URL in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Get the Full URL in PHP

When building web applications, distinguishing between the script's physical location and the address a user typed into their browser is a common source of confusion. Developers often encounter scenarios where .htaccess rules or framework rewrites map a clean, readable URL to an actual file path deep within the directory structure. If you rely on the traditional approach of constructing a link using $_SERVER['HTTP_HOST'] and $_SERVER['PHP_SELF'], you are inadvertently retrieving the server-side path rather than the client-facing URL.

Consider the specific implementation provided in your source material:

$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

This snippet concatenates the host and the script name. While this works for a standard directory listing where the file path matches the URL, it breaks immediately when rewrite rules are active. In such environments, $_SERVER['PHP_SELF'] returns the internal filesystem path (e.g., /products/item-123.php) while the browser displays a clean route (e.g., /products/123). Using the raw self-path results in broken redirects or incorrect canonical tags, as you end up generating links that point to the server's internal structure rather than the user's view.

To capture exactly what appears in the navigation bar—the "full URL" from the client's perspective—you must bypass the reconstructed path and read directly from the incoming request headers. The most robust method involves checking for the HTTP_HOST header combined with the script name, but more importantly, utilizing the REQUEST_URI variable when a base URL is known, or simply parsing the Host header if you need to reconstruct it dynamically.

However, there is a nuance regarding HTTPS that this simple concatenation misses. If your site uses SSL termination at a load balancer or reverse proxy, $_SERVER['HTTPS'] might not reflect the actual protocol used by the client. A safer pattern for generating the full URL involves checking if the request is secure:

$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$path = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';

// Ensure the path doesn't start with a slash if we are appending to host
if (!empty($path) && $path[0] === '/') {
    $full_url = $protocol . '://' . $host . substr($path, 1);
} else {
    $full_url = $protocol . '://' . $host . $path;
}

This logic ensures you are capturing the URL exactly as the browser rendered it, regardless of how .htaccess has rewritten the final destination script. This is critical for generating <link rel="canonical"> tags or constructing absolute URLs for internal navigation without risking 404 errors caused by exposing backend file extensions.

If you are working within a Laravel application, the framework abstracts this complexity away. You can simply use url()->full() or construct it via request()->fullUrl(). These methods automatically handle the protocol, host, and query string, ensuring consistency even when complex routing rules are in place. For those maintaining legacy PHP code outside of frameworks, understanding the difference between the filesystem path (PHP_SELF) and the request URI is essential for maintaining URL integrity.

Our network