Skip to content

WordPress redirect loop behind Cloudflare: HTTPS detection and plugin conflicts

ERR_TOO_MANY_REDIRECTS on a WordPress site behind Cloudflare is almost always broken HTTPS detection under Flexible SSL, or two plugins fighting over the same redirect. Here is how to find the layer at fault and fix it.

A WordPress site behind Cloudflare that throws ERR_TOO_MANY_REDIRECTS is almost always an HTTPS detection problem. Cloudflare terminates TLS at the edge and — in Flexible mode — talks to your origin over plain HTTP. WordPress sees an HTTP request while its site URL says https://, issues a redirect to HTTPS, Cloudflare fetches that URL over HTTP again, and the loop never ends. The fix is to encrypt the origin leg with Full (strict), or to teach WordPress to trust the X-Forwarded-Proto header — and to make sure exactly one layer owns the HTTP-to-HTTPS redirect.

What’s happening

Four parties can issue a redirect on a proxied WordPress site, and they answer in a fixed order. First, the browser cache: a 301 seen earlier replays from disk with no network request at all. Second, the Cloudflare edge: Always Use HTTPS fires before Redirect Rules, and both fire before the request touches cache or origin — but only on hostnames with a proxied (orange-cloud) DNS record. Third, the origin web server: an .htaccess rewrite or nginx vhost rule. Fourth, WordPress itself: redirect_canonical(), plus whatever an SSL or redirection plugin bolts on.

A loop means two of those layers disagree about the canonical scheme. The classic case is Flexible SSL: the edge insists on https:// for visitors while the origin only ever receives http://, so any origin-side or WordPress-side “force HTTPS” logic redirects forever. The second common case is plugin conflicts — an SSL plugin and a redirection plugin (or a security plugin) each adding their own scheme redirect with slightly different ideas about the final URL.

What to check first

  • SSL/TLS mode in the Cloudflare dashboard. Flexible is the prime suspect; see TLS modes explained.
  • siteurl and home in WordPress — do they match the scheme and host visitors use?
  • Active plugins that touch redirects: Really Simple SSL, Redirection, security suites with a force-HTTPS toggle.
  • Test with curl, not a browser. Browsers replay cached 301s, so the loop can look alive after you have already fixed it.

How to verify

Walk the chain and watch for the same Location repeating:

curl -sIL --max-redirs 5 https://example.com/ | grep -iE 'HTTP/|location|cf-ray|x-redirect-by'

Then take one hop at a time and identify the issuer:

curl -sI https://example.com/ | grep -iE 'HTTP/|location|server|cf-ray|cf-cache-status|x-redirect-by'

Read it like this: x-redirect-by: WordPress means the application issued it. A 301 with server: cloudflare, a cf-ray, and no cf-cache-status is edge-generated (Always Use HTTPS or a Redirect Rule). A redirect with cf-cache-status: HIT is a stale copy served from the edge cache. No cf-ray at all means Cloudflare is not in the path.

Now ask the origin directly, bypassing Cloudflare (add -k if it runs an Origin CA certificate):

curl -skI --resolve example.com:443:203.0.113.10 https://example.com/ | grep -iE 'HTTP/|location'
curl -sI  --resolve example.com:80:203.0.113.10  http://example.com/  | grep -iE 'HTTP/|location'

If the HTTP probe returns a 301 to HTTPS and the HTTPS probe returns 200, the origin is sane and the loop lives in the Cloudflare-to-origin leg. Finally, check what WordPress believes:

wp option get siteurl && wp option get home

The fix

  1. Move SSL/TLS mode to Full (strict). Install a valid certificate on the origin — an Origin CA certificate is free and takes minutes — then switch the zone from Flexible. This makes the Cloudflare-to-origin request HTTPS, so WordPress detects the scheme natively and the loop dies. This is the correct fix, not a workaround; Cloudflare’s own SSL documentation discourages Flexible.

  2. Align the WordPress URLs. Set both to the canonical HTTPS form: wp option update siteurl https://example.com and the same for home (or fix WP_HOME / WP_SITEURL in wp-config.php if they are defined there — constants override the database).

  3. If the origin genuinely cannot do TLS, add proxy-aware HTTPS detection. In wp-config.php, above the wp-settings.php require line:

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

    WordPress then treats edge-HTTPS requests as HTTPS even though the origin leg is HTTP. Treat this as a stopgap while you arrange Full (strict).

  4. Pick one owner for the scheme redirect. Keep Always Use HTTPS at the edge and remove the duplicates: the force-HTTPS toggle in SSL plugins, hand-added .htaccess scheme rewrites, and any Redirection-plugin rule doing the same hop.

  5. Clear every cache afterwards. New private window (or clear the site’s browser data), purge the Cloudflare zone if a 301 was cached at the edge, and flush the page-cache plugin.

When this fix is wrong

  • The loop only hits /wp-admin/ or the login page. That is a cookie or FORCE_SSL_ADMIN problem, not scheme detection — a different article’s territory.
  • The Location header alternates hosts, not schemes (www to apex and back). That is host canonicalization fighting between an edge rule and WordPress, not HTTPS detection.
  • The DNS record is grey-cloud (DNS only). Cloudflare is not proxying, so nothing here applies — the loop is entirely origin plus CMS, and the general playbook in WordPress too-many-redirects is the right one.

Common mistakes

  • Retesting in the same browser tab. The cached 301 replays and you conclude the fix failed. Always confirm with curl.
  • Setting $_SERVER['HTTPS'] = 'on' unconditionally instead of keying on X-Forwarded-Proto. That breaks any legitimate HTTP path and hides real scheme bugs.
  • Fixing wp-config.php but leaving a bare .htaccess force-HTTPS rule. Under Flexible the origin never sees HTTPS, so an .htaccess rule testing %{HTTPS} still loops. Condition it on X-Forwarded-Proto or delete it.
  • Running two SSL plugins at once, each rewriting URLs and redirecting.
  • Editing siteurl in the database while WP_SITEURL is defined in wp-config.php — the constant wins and the edit silently does nothing.

Prevention

Standardize on Full (strict) for every new zone and never enable Flexible as a shortcut. Decide the redirect owner once — edge handles scheme, WordPress handles slugs — and write it down. Keep the X-Forwarded-Proto block in your base wp-config.php; it is harmless under Full (strict) and saves you when someone downgrades the TLS mode. And put a four-variant curl walk (http/https times www/apex) in the deploy checklist so a new loop is caught before visitors report it.

If redirects keep fighting each other across Cloudflare, the origin, and the CMS, Stack Harbor untangles that chain as part of Managed Cloud Ops — book an infrastructure review.