PHP Get Site URL Protocol - http vs https

PHP Get Site URL Protocol - http vs https

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

Stefan Bogdanescu

Founder & Senior Architect

PHP Get Site URL Protocol: Handling HTTP vs HTTPS Correctly

Determining the correct protocol (HTTP or HTTPS) for your site is a classic dilemma when migrating to SSL. You might have a function like this:

function siteURL() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'].'/';
    return $protocol.$domainName;
}
define( 'SITE_URL', siteURL() );

Alternatively, you might see a simpler version that assumes HTTP by default:

function siteURL() {
    $protocol = 'http://';
    $domainName = $_SERVER['HTTP_HOST'].'/';
    return $protocol.$domainName;
}
define( 'SITE_URL', siteURL() );

The core question is whether the server automatically converts URLs to HTTPS or if you must explicitly check for it. The short answer is that browsers and servers handle protocol redirection based on configuration, but your PHP code cannot rely on automatic conversion to determine which protocol is currently active.

How $_SERVER['HTTPS'] Works

The logic in the first example relies on $_SERVER['HTTPS']. This variable is set by the web server based on the incoming request's connection state. If the connection is secure, most servers set this to 'on'. If it’s unencrypted, it is either unset or 'off'.

The second condition in the logic checks $domainName against port 443:

$_SERVER['SERVER_PORT'] == 443

This is a critical safety net. Many production servers are configured to listen on port 80 for HTTP and redirect to HTTPS, or they use reverse proxies (like Nginx in front of PHP-FPM). In these setups, the actual TLS handshake happens before the request reaches your application layer, so $_SERVER['HTTPS'] might be unset even though the user is browsing over SSL. Checking the port ensures you detect the secure context regardless of how the proxy or load balancer handles the headers.

Why You Can't Assume HTTP Default

The second function assumes HTTP by default. This approach works only if your server never serves content over HTTPS, which is rarely the case for modern sites. Even if a browser redirects an unencrypted request to a secure one, your PHP code executes after the redirect has occurred. At that point, $_SERVER['HTTPS'] will reflect the current connection state (secure), and relying on a hardcoded 'http://' would result in broken links or mixed content errors.

You cannot rely on the browser to "fix" the protocol inside your application logic. If you generate an image tag like <img src="SITE_URL/logo.png"> and SITE_URL returns http://, but the site is served over HTTPS, the browser will block the request as mixed content.

The Red Herring of Automatic Conversion

A common misconception is that if a user enters http://example.com into their browser, the server automatically serves them https://. While true for redirect rules (like .htaccess or Nginx configs), this does not change the environment variables available to your PHP script in a way that allows you to infer the protocol solely from the request URL without checking the connection state.

Your code must detect the current connection security. The first function provided is the correct approach because it explicitly checks for both the HTTPS header and the port number. It handles edge cases where the server might be behind a load balancer or using SSL termination at a different layer.

Best Practice Implementation

For robust applications, always verify the protocol based on the active connection state rather than assuming a default. Here is a refined version of the logic that covers the most common server configurations:

function getSiteURL() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
    
    // Check if the port is 443, which implies HTTPS even if the header is missing (e.g., behind a proxy)
    if ($_SERVER['SERVER_PORT'] == 443) {
        $protocol = 'https://';
    }

    return $protocol . $_SERVER['HTTP_HOST'];
}

This ensures that whether you are running on a direct connection or behind a reverse proxy, your application always generates the correct base URL. This prevents mixed content warnings and ensures that resources like CSS, JS, and images load correctly over secure connections.

Our network