Infinite redirect issue with Wordpress admin and Cloudflare (aka err_too_many_redirects)

Eralp Bayraktar
2 min readAug 25, 2020

Very short answer: Add the following snippet to the top of your wp-config.php file.

if (strpos($_SERVER[‘HTTP_X_FORWARDED_PROTO’], ‘https’) !== false)
$_SERVER[‘HTTPS’]=’on’;
else
$_SERVER[‘HTTPS’]=’off’;

Longer answer:

Photo by Fikret tozak on Unsplash

Hi! I recently was setting up a WordPress website for our CV Application [https://thecvapp.com](https://thecvapp.com) and suddenly wp-admin URL started redirecting to itself, to the same exact URL. And when this happens enough times, Google Chrome just spits out the “err_too_many_redirects” error, as there’s probably an infinite loop. I’m using Cloudflare to manage my SSL and all the caching, plus some more.

Why is this happening?

My Cloudflare SSL setting was set to “Flexible” mode, this works as following, Browser sends an SSL request, Cloudflare captures this and decrypts, then sends an HTTP request to your server. It’s called “terminating” the SSL at Cloudflare level.

Your webserver in the end receives an HTTP request. This is very easy to setup since your server doesn’t need to manage private keys and encryption etc., but less secure indeed. Everything is a trade-off after all!

What’s the solution?

The solution is either terminate the SSL at wordpress level and set up the SSL correctly, BUT the easier solution is adding the following snippet to the top of your `wp-config.php`.

if (strpos($_SERVER[‘HTTP_X_FORWARDED_PROTO’], ‘https’) !== false)
$_SERVER[‘HTTPS’]=’on’;
else
$_SERVER[‘HTTPS’]=’off’;

What this does is, checks an HTTP header put by Cloudflare, to see if the connection had been an SSL and had been already secured at the browser level. If so, it’s okay to accept even though the current request is HTTP.

Photo by Fauzan Saari on Unsplash


Congratulations! Keep blogging like a champion, and please follow me on [@EralpBayraktar](https://twitter.com/EralpBayraktar) :)

--

--