How to Check if the Current Request Uses HTTPS in PHP (Including Behind a Proxy)

isset($_SERVER["HTTPS"]) is not quite enough. Here is a check that also handles IIS, non-standard ports and sites behind Cloudflare or a load balancer.

To check whether the current request uses HTTPS in PHP, test that $_SERVER['HTTPS'] is set and isn’t off, or that the port is 443. If your site sits behind Cloudflare, a load balancer or another proxy that handles SSL, PHP sees plain HTTP, so you also need to check the X-Forwarded-Proto header, and only trust it when the request really came from your proxy. In WordPress, use is_ssl().

Originally published in March 2011. Rewritten in December 2026 for HTTPS-everywhere sites and proxies.

The basic check

function is_https(): bool
{
    if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        return true;
    }
    return isset($_SERVER['SERVER_PORT']) && (int) $_SERVER['SERVER_PORT'] === 443;
}

Checking only isset($_SERVER['HTTPS']), as many old snippets do, gives the wrong answer on IIS, which sets HTTPS to off for plain HTTP requests.

Behind a proxy or CDN

When Cloudflare, AWS, another CDN, or Nginx in front of Apache terminates SSL, the connection to PHP is HTTP. The proxy tells you the original scheme in a header:

function is_https_behind_proxy(array $trustedProxies): bool
{
    if (is_https()) {
        return true;
    }
    $remote = $_SERVER['REMOTE_ADDR'] ?? '';
    if (in_array($remote, $trustedProxies, true)) {
        return strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https';
    }
    return false;
}

Anyone can send an X-Forwarded-Proto header, so don’t trust it from arbitrary clients. If your proxy’s IP range changes, like Cloudflare’s published ranges, compare against the range rather than single IPs, or let your web server set HTTPS=on for trusted proxies so PHP doesn’t have to.

Frameworks handle this for you once you configure trusted proxies: Laravel’s TrustProxies middleware, Symfony’s trusted_proxies setting and CakePHP’s trustProxy option.

WordPress

Use the built-in function:

if (is_ssl()) {
    // HTTPS
}

If WordPress behind a proxy thinks it’s on HTTP and you get redirect loops or mixed content warnings, add this to wp-config.php above the “That’s all, stop editing” line:

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    $_SERVER['HTTPS'] = 'on';
}

Redirect HTTP to HTTPS

Do the redirect in the web server or CDN rather than PHP, so it happens before any code runs. For Apache rules that handle HTTPS and www together, see redirecting non-www to www with .htaccess.

Build URLs with the right scheme

$scheme = is_https() ? 'https' : 'http';
$url = $scheme . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

For canonical tags and emails, use a fixed base URL from config instead of HTTP_HOST, which comes from the request and can be faked.

Common questions

Why does $_SERVER[‘HTTPS’] not exist on my HTTPS site?

Your host or CDN terminates SSL before the request reaches PHP. Check HTTP_X_FORWARDED_PROTO from your trusted proxy, or ask your host to set HTTPS for you.

Is checking port 443 enough?

Not always. Some servers run HTTPS on other ports, and behind a proxy the port is often 80.

What about the REQUEST_SCHEME variable?

Apache 2.4 sets $_SERVER['REQUEST_SCHEME'], but other servers may not, and it has the same proxy problem. Treat it as a hint, not the only check.