Fix Cloudflare’s https redirect loop on WordPress

TL;DR: Adding $_SERVER['HTTPS'] = 'on'; to wp-config.php seemed to stop the redirect loop.

The problem

So basically first post, and first thing that I’ve encounter when setting up WordPress alongside Cloudflare’s Flexible SSL option. For whatever reason, on other websites that have the same configuration (but or not on WordPress) everything seemed to work out fine.

Let me show you my current configuration. So, I have an Nginx custom configuration file for weeklytab.com where I basically have two entries regarding the domain.

An redirect block from www.weeklytab.com to https://weeklytab.com


server {
listen 80;
server_name www.weeklytab.com;
return 301 https://weeklytab.com$request_uri;
}

And an server block that should handle weeklytab.com


server {
listen 80;
server_name weeklytab.com;
....
}

OK, so you might have the same thing on your end if you’re using Nginx. Looking at Cloudflare’s documentation, they say that when using a Flexible SSL, they are somehow a proxy over a client HTTP request to you. In other words, they provide SSL encryption to the user but they send traffic over regular http (or port 80) to you. Everything seemed fine but I ended up with a redirection loop.

What I’ve tried

1) On Cloudflare’s support page they say that adding a page rule to force everything over to HTTPS. Unfortunately, that didn’t work.
2) Also tried to change the second server block from port 80 to port 443 (to make it listen to HTTPS requests). Unfortunately, that didn’t work either.
3) Also tried using Cloudflare’s “Automatic HTTPS Rewrites” option which you will find under the Crypto tab. That did some changes (as in changed all my links in the source code from http to https) but the redirection was still there.

What did make it work

So to make it work, I added in the wp-config.php file this: $_SERVER['HTTPS'] = 'on';

Did seemed to do the trick.

Quick note here. Before everything else, check that your HOME and SITEURL point to https://yoursite.com and not http://yoursite.com

Leave a reply:

Your email address will not be published.