How to correctly force SSL on WordPress via wp-config.php?

How to correctly force SSL on WordPress via wp-config.php?

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

Stefan Bogdanescu

Founder & Senior Architect

Fixing SSL Force Logic When Using .htaccess Redirections

When you configure WordPress to force SSL via wp-config.php, the standard advice is straightforward: define FORCE_SSL_ADMIN and FORCE_SSL_LOGIN. This tells WordPress to redirect administrative requests to HTTPS regardless of what the browser sends. However, this setting interacts poorly with server-level redirection rules like those found in .htaccess files that enforce https://www.domain.com for all traffic.

The core issue arises because your .htaccess rules likely redirect HTTP requests to HTTPS before PHP executes. If the web server successfully performs a 301 or 302 redirect, the client receives the response and closes the connection. When the user then clicks through to the admin panel, the browser sends a new request over HTTP (if they didn't click the redirect) or HTTPS. In many modern setups involving load balancers or reverse proxies, this causes confusion regarding the $_SERVER['HTTPS'] variable.

If your environment is behind a proxy that terminates SSL before passing traffic to the application server, the incoming request might appear as plain HTTP to PHP. The proxy typically sets the header HTTP_X_FORWARDED_PROTO to https. Without handling this, WordPress checks $_SERVER['HTTPS'], sees it as empty or 'off', and fails to recognize the secure connection. This triggers unnecessary redirects or breaks the admin login flow entirely when combined with FORCE_SSL_ADMIN.

Evaluating the Code Variations

You have presented three variations for correcting this behavior in wp-config.php. The goal is to ensure that if a proxy indicates an HTTPS connection, PHP acknowledges it by setting $_SERVER['HTTPS'] to 'on' and ensuring $_SERVER['SERVER_PORT'] reflects port 443. This prevents WordPress from generating redirect loops or failing to validate the secure context.

Option 1 includes both headers but lacks an isset() check.
While functionally similar, checking isset() is a best practice. If the header is missing entirely (which happens when requests are direct and not proxied), accessing it without a check can trigger warnings or unexpected behavior in stricter PHP configurations.

Option 2 uses isset() but lacks curly braces and sets the port.
The absence of curly braces is syntactically valid in PHP, provided there is only one statement following the if. However, omitting them reduces readability and makes debugging harder if you decide to add logic later. More importantly, setting $_SERVER['SERVER_PORT'] = 443 is often redundant if the upstream proxy already passes the correct port information, but it ensures consistency in environments where the proxy might strip that data.

Option 3 combines isset(), curly braces, and the port adjustment.
This is generally the most robust approach for production environments behind a reverse proxy. The isset() check prevents warnings if the header is absent. The curly braces enforce a block structure, which is clearer when managing multiple server variables. Explicitly setting $_SERVER['SERVER_PORT'] to 443 ensures that any internal logic relying on the port number sees a secure context, even if the raw request arrived on an unencrypted interface from the proxy's perspective.

Implementation and Trade-offs

When implementing this fix in wp-config.php, ensure the code is placed near the top of the file, before any database configuration. Using Option 3 provides the safest net: it handles both direct HTTPS connections and proxied connections correctly without relying on the assumption that $_SERVER['HTTPS'] is always populated by the web server.

One potential pitfall is redirect loops. If FORCE_SSL_ADMIN is active and your application thinks it is running over HTTP (because $_SERVER['HTTPS'] was not corrected), WordPress will issue a 302 redirect to HTTPS. If your .htaccess rules are also forcing HTTPS, you might see multiple redirects in the network tab. By normalizing $_SERVER['HTTPS'] and $_SERVER['SERVER_PORT'] upfront, you align PHP's internal state with the actual connection security level, allowing WordPress's built-in SSL logic to function as intended without fighting against server headers.

For complex setups involving load balancers or specific proxy configurations, this manual override of the superglobals is a reliable fallback mechanism. It ensures that application-level code behaves consistently regardless of whether the connection was encrypted at the network edge or if the proxy simply flagged it as such via headers.

Our network