Square CloudSquare Cloud

ERR_TOO_MANY_REDIRECTS: redirect loops explained

ERR_TOO_MANY_REDIRECTS is a redirect loop: each response sends the browser to another address, in a circle, until it gives up. The most common cause is an app forcing HTTPS behind a proxy that already terminates HTTPS and forwards the request over plain HTTP.

Updated 8 min read

Learning objectives

After reading this article you will be able to:

  • Recognize the error in the browser and trace the redirect chain with curl
  • Fix the classic HTTPS loop behind a TLS-terminating proxy
  • Avoid www, trailing slash, cookie and CDN SSL mode conflicts
On this page

You turn on HTTPS, ship the new version, open the site and the browser gives up before showing anything. No error in the app logs, no 500: just a page saying the address redirected too many times.

The server is not down. It answers, and fast, but every response sends the browser to another address that sends it back. There are almost always two layers disagreeing about which address is the right one, and finding both is the whole job.

What does the error look like?#

In Chrome, the page reads:

text
This page isn't working
example.com redirected you too many times.
Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS

Chromium-based browsers such as Edge show the same code. Firefox has its own page for the problem and also points to cookies as a possible cause.

A redirect is a response with a 3xx status (301, 302, 307 or 308) and a Location header holding the next address. The browser follows it on its own, without asking. To avoid getting stuck forever, it counts the hops: the Fetch standard returns a network error once the count reaches 20, and Chrome and Firefox use that same limit.

The loop can have two steps (A sends to B, B sends to A) or one (A sends to A). It can also be a growing chain, like /page becoming /page/ and then /page//. In every case, no response is ever an actual page.

How do you see the redirect chain?#

The browser hides the steps. curl shows each one:

bash
curl -sS -I -L --max-redirs 3 http://example.com/ | grep -iE '^(HTTP|location)'

-I sends HEAD requests and prints only headers. -L follows redirects and, combined with -I, prints the headers of every response in the chain. --max-redirs cuts the loop short, because curl's default is to follow up to 50. On Windows, run it in Git Bash or WSL, which ship with grep.

An HTTPS loop behind a proxy looks like this:

text
HTTP/1.1 301 Moved Permanently
Location: https://example.com/
HTTP/2 301
location: https://example.com/
HTTP/2 301
location: https://example.com/
HTTP/2 301
location: https://example.com/
curl: (47) Maximum (3) redirects followed

Compare each Location with the address that was just requested:

  • Location repeats the same address over HTTPS: the app doesn't notice the request already arrived over HTTPS. This is the most common case, covered in the next section.
  • It alternates between example.com and www.example.com: two layers disagree about the primary domain.
  • It alternates between /page and /page/: one rule adds the trailing slash and another removes it.
  • It alternates between /login and /account: the problem is in the session or a cookie.

Some apps answer HEAD differently from GET. If the chain looks odd, repeat it with GET and discard the body: curl -sS -L --max-redirs 3 -o /dev/null -D - http://example.com/. In the browser, open the DevTools Network tab, check "Preserve log" and reload to see each 301 or 302 with its Location.

Why does forcing HTTPS in the app cause a loop behind a proxy?#

On most hosting setups, HTTPS ends before your app. A CDN, a load balancer or a reverse proxy accepts the encrypted connection, terminates TLS and forwards the request to the app over plain HTTP on the internal network.

So, from the app's point of view, every request arrives over HTTP. If the code has a rule saying "if this isn't HTTPS, redirect to HTTPS", it fires every time:

  1. The browser requests https://example.com/.
  2. The proxy terminates TLS and forwards the request over HTTP.
  3. The app sees HTTP and answers 301 with Location: https://example.com/.
  4. The browser requests https://example.com/ again, and the cycle restarts.

The Django docs describe this exact symptom: if turning on SECURE_SSL_REDIRECT causes infinite redirects, the site is probably behind a proxy and can't tell which requests are secure. The WordPress docs give the same warning about FORCE_SSL_ADMIN behind a proxy that provides SSL.

There are two fixes, and the first one is the simplest:

  1. Don't redirect in the app. If the layer in front already redirects HTTP to HTTPS, the app's rule is redundant. Turn it off.
  2. Teach the app to trust the proxy. Many proxies report the original protocol in a header, usually X-Forwarded-Proto, and frameworks have a setting to read it. Use it only if your proxy sends that header and overwrites whatever the client sent; otherwise anyone can forge it.

In Express, the second fix looks like this:

js
import express from "express";

const app = express();

// Without this line, req.secure is always false behind the proxy, and
// the middleware below redirects every request: a loop.
// With it, Express reads the X-Forwarded-Proto sent by the proxy.
app.set("trust proxy", 1);

app.use((req, res, next) => {
  if (req.secure) return next();
  res.redirect(308, `https://${req.headers.host}${req.originalUrl}`);
});

app.get("/", (req, res) => res.send(`ok, protocol: ${req.protocol}`));

app.listen(Number(process.env.PORT) || 3000, "0.0.0.0");

If the loop persists even with trust proxy, the proxy isn't sending the header you assumed. Print the incoming headers (a temporary console.log(req.headers)) and check before trusting any of them.

The same idea in other frameworks:

Framework What forces HTTPS What makes the app recognize the proxy's HTTPS
Express A custom middleware using req.secure app.set("trust proxy", 1)
Django SECURE_SSL_REDIRECT = True SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
Rails config.force_ssl = true config.assume_ssl = true
WordPress FORCE_SSL_ADMIN or a site address starting with https:// In wp-config.php, $_SERVER['HTTPS'] = 'on' when X-Forwarded-Proto contains https

Django lists the conditions for using SECURE_PROXY_SSL_HEADER: the app must sit behind a proxy that strips that header when it comes from the client and only sets it for requests that arrived over HTTPS. Rails' assume_ssl makes the app treat every request as HTTPS, so it only fits when nothing reaches the app without going through the proxy.

What are the other common causes?#

The CDN's "Flexible" SSL mode#

Some CDNs offer a mode where the visitor uses HTTPS up to the edge, but the edge connects to your origin over HTTP. At Cloudflare, it's called Flexible. If the origin redirects all HTTP to HTTPS, every response goes back to the edge, which asks over HTTP again. Cloudflare's documentation recommends removing the origin redirect or switching to Full (strict) mode, with a valid certificate on the origin.

www and non-www fighting#

One layer redirects example.com to www.example.com, and another (the registrar's forwarding, a CDN rule, the web server) does the opposite. Pick one primary address and redirect to it in a single place. Also check the settings that store the app's public URL, such as the site addresses in WordPress, because the app redirects to them.

The trailing slash#

The framework adds / to the end of every URL and the web server in front removes it, or the other way around. curl shows Location alternating between the two forms. Keep the rule in one layer. In Next.js, for example, the default redirects /about/ to /about, and trailingSlash: true reverses that.

Cookies and sessions#

A middleware sends users without a session from /account to /login, and the login page sends users with a session back to /account. If the two checks disagree (one only checks that the cookie exists, the other validates its content), an expired or corrupted cookie traps the user between them. That's why Chrome suggests deleting cookies: it fixes things for that person, but the real fix is making both routes use the same check and deleting the cookie when it's rejected.

Try a private window. If it works there and not in your normal window, the cause is something the browser stored: cookies or a cached redirect.

How do you keep the loop from coming back?#

  • One place for each rule. The HTTPS redirect lives in a single layer, ideally the first one to receive traffic. The same goes for the primary domain and the trailing slash.
  • Test with 302 or 307 before 301 or 308. RFC 9110 lists 301 and 308 among the responses that can be cached heuristically, and a browser that cached the wrong redirect keeps looping after the fix (see how Cache-Control works). Switch to the permanent code once the chain is right.
  • Run curl after every change. A healthy chain has one or two steps and ends in a 200.
  • HSTS last. Turn on Strict-Transport-Security only after HTTPS works without a loop, because the browser then goes straight to HTTPS for the whole max-age. See HTTP security headers and the difference between HTTP and HTTPS.
  • HTTPS links. An app that doesn't notice HTTPS also builds links and assets with http://, which causes mixed content even without a loop.

If the browser never even gets a redirect and the page says the connection was refused or timed out, that's a different problem: see ERR_CONNECTION_REFUSED and ERR_CONNECTION_TIMED_OUT.

How to do this on Square Cloud#

On Square Cloud, the HTTP to HTTPS redirect already happens on the network, before your app. Your web app listens on 0.0.0.0 on the port in the PORT variable and receives traffic on port 80, and anyone who types http:// is taken to https:// without any rule in your code. So the fix for the classic loop is the first one on the list: don't force HTTPS in the app. In Django, leave SECURE_SSL_REDIRECT off; in Rails, leave config.force_ssl off; in Express, remove the redirect middleware.

If your framework needs to know the original request came over HTTPS (to build links or mark cookies as Secure), print the headers that reach the app and check them in the dashboard's real-time logs before configuring any proxy trust.

Every published web app gets a *.squareweb.app subdomain with HTTPS and automatic TLS. Your app's headers, such as HSTS, pass through untouched, and the CDN respects the Cache-Control you set. From the Standard plan, you connect a custom domain by creating the DNS records shown in the dashboard at your registrar; the domain feature includes cache purge, useful when an old redirect got stored at the CDN. See the step by step in custom domains.

Network

Custom domains, automatic TLS and a global CDN for your app

Custom domains

FAQs

Does clearing cookies fix ERR_TOO_MANY_REDIRECTS?

Only when the cause is an expired or invalid cookie that makes the app redirect. If the loop comes from the server configuration, it comes right back. Try a private window: if the error happens there too, the problem is on the server.

Why does the error persist after I fixed the server?

The browser may have cached a 301 or 308 redirect. Test in a private window or with curl, and clear the browser cache. While you test new rules, use 302 or 307.

How many redirects does a browser follow?

Chrome and Firefox give up after 20 redirects in a row, the same limit as the Fetch standard. curl with -L follows up to 50 by default.

What does Cloudflare's Flexible SSL mode have to do with redirect loops?

In Flexible mode, the CDN connects to your origin over HTTP. If the origin redirects all HTTP to HTTPS, every response sends the visitor back to the CDN, which asks over HTTP again. Remove the origin redirect or switch to Full (strict) with a certificate on the origin.

Next articleCache-Control headers explainedCache-Control is the HTTP header that sets how long browsers and CDNs keep a response. Learn max-age, no-cache, immutable and ready-made recipes for each file type.

More in Networking and domains

See all 31 articles