WordPress in HTTPS, causing Redirect Loops

The answer to this came in part from this answer, which linked to the Codex, advising the following snippet to be placed at the top of the wp-config file:

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
       $_SERVER['HTTPS']='on';

Unfortunately, this didn’t quite solve it for me, but I noticed it worked if I removed the if and just always set $_SERVER['HTTPS']='on';. If you want to always force SSL on your site, then that should do the trick.

If you want to allow both http and https, however, you’ll need to adjust your nginx config. Add the following line:

proxy_set_header X-Forwarded-Proto $scheme;

That’s the header the if condition above is looking for. By setting nginx to pass the requested scheme (either http or https) along as the X-Forwarded-Proto, you’re letting WordPress see what that original scheme was so that it knows how to behave.

Once you’ve done that, your WP site should work properly over both http and https.

Leave a Comment