November 23, 2026 · 10 min read · Thomas Osayi
Too many redirects in Next.js (ERR_TOO_MANY_REDIRECTS): find the loop and fix it
How to fix ERR_TOO_MANY_REDIRECTS in Next.js: auth loops in proxy.ts or middleware, missing cookies, www vs apex on Vercel, trailing slashes and Cloudflare SSL.
"This page isn't working. example.com redirected you too many times." That message, with ERR_TOO_MANY_REDIRECTS underneath, is one of the most frustrating errors a founder can hit, because the site does not crash and the logs often look normal. The browser simply gives up after being bounced between URLs.
The good news is that a redirect loop always has a cause you can see if you look at the right thing. Two rules are disagreeing, and your job is to find both of them. This post covers the causes I check in a Next.js app, in the order I check them, with concrete steps for each. If your auth provider is Supabase and the problem is the redirect after email sign-in rather than a loop, "Supabase auth redirect URL not working?" is the closer match.
What does ERR_TOO_MANY_REDIRECTS mean in a Next.js app?
It means the browser followed a chain of redirects that never reached a real page, usually because two rules send the request back and forth.
A redirect is a response with a 3xx status and a Location header that says "go here instead". Browsers follow these automatically, but they stop after a limit and show the error. In a Next.js app on Vercel, redirects can come from several places, and they run in a specific order:
- Cloudflare or another proxy in front of your site, if you use one.
- Vercel's domain settings, such as redirecting www to the apex domain or the reverse.
- redirects in next.config.js, which Next.js runs before your proxy.
- Your proxy.ts file (middleware.ts in older projects).
- Code in pages, layouts and server actions that calls redirect.
- Client code that pushes the user to another route after checking a session.
A loop needs two of these pointing at each other. An auth check sends signed out users to /login, and something on /login sends them back. Or Vercel sends www to apex and a DNS forward sends apex to www. Fixing one side without finding the other just moves the loop.
How do you find which redirect is looping?
Trace the chain of Location headers, either in the browser's Network tab or with a single curl command.
In Chrome or Edge, open developer tools, go to Network, tick Preserve log, and load the page. You will see a list of requests with 307, 308 or 301 statuses. Click each one and read two things: the request URL and the Location response header. The pattern usually jumps out, for example /dashboard to /login to /dashboard, or https://www to https://apex to https://www.
From a terminal, curl -sIL followed by your URL prints every response's headers in order. Look for Location lines repeating, and for headers that reveal who sent the redirect. A response with a server header from Cloudflare, or one that arrives before any Next.js headers, tells you the redirect did not come from your code.
Then test variations:
- A private window, with no cookies. If the loop disappears, it involves a session cookie.
- The vercel.app URL of the deployment instead of your domain. If that works, the loop lives in domain or DNS settings.
- Local development. If it loops locally too, it is in your code.
Those three tests narrow the cause to one area within minutes.
Is your proxy (formerly middleware) causing an auth loop?
Very often, yes. An auth check that runs on every route, including the login page, will redirect signed out users to login forever.
In Next.js 16, the middleware file convention was deprecated and renamed to proxy. New projects have a proxy.ts file in the project root (or in src) exporting a function called proxy; older projects still have middleware.ts. Next.js provides a codemod to rename it. Either way, the file runs before your routes render, and the docs warn that without a matcher it runs on every request, including static files and images.
The classic loop looks like this: the proxy checks for a session, finds none, and redirects to /login. The request for /login also goes through the proxy, which finds no session and redirects to /login again. The fixes:
- Add a matcher so the proxy only runs on protected paths, for example /dashboard and /account and their sub-paths.
- Or, if it must run broadly, return early for public routes such as /login, /signup, /auth/callback and /reset-password.
- Exclude _next/static, _next/image and files like favicon.ico, so assets are not redirected.
The opposite loop also happens: a signed in user visiting /login is redirected to /dashboard, and the dashboard's own server check thinks they are signed out and sends them back. That points to the next cause.
Why does the loop only start after signing in?
Because the session cookie is either not being set or not being read in the place that makes the redirect decision.
In this pattern, the sign-in succeeds, the app redirects to the dashboard, and something on the server does not see a session, so it sends the user to login, which sees a session in the browser and sends them back. Common reasons:
- The cookie is set in client code, but the server check reads a different cookie, or a cookie that only the server can set.
- The cookie is set on a response that is then thrown away. In a proxy, cookies must be set on the response you actually return, for example the NextResponse.next() or redirect object, not a new one created later.
- The cookie has the Secure flag and you are testing on plain http, so the browser never stores it.
- The cookie's domain is set for the apex domain, but the user is on www, or the reverse.
- The auth library refreshes tokens in the proxy, but the refreshed cookies are not copied to the response, so the next request carries an expired session.
Check in developer tools under Application, then Cookies. After signing in, the session cookie should exist for the exact domain you are on. Then check that the proxy and your server code read it the same way. If your app uses Firebase Authentication with server checks, "Firebase Authentication in the Next.js App Router: how to do it properly" goes deeper into that setup.

Can www and apex domain redirects loop on Vercel?
Yes, when the redirect between www and the bare domain is set in two places that disagree.
Vercel lets you add a redirect between domains in Project Settings, under Domains: you edit the domain you want to redirect from and choose the target in the Redirect to dropdown. Vercel's docs recommend www as the primary domain with the apex redirecting to it, though apex as primary also works. The problem starts when another layer has its own opinion:
- Your registrar's domain forwarding sends example.com to www.example.com while Vercel redirects www to example.com.
- A redirect in next.config.js or vercel.json forces one host while the Vercel domain setting forces the other.
- Proxy code that rewrites the host based on an environment variable, such as NEXT_PUBLIC_SITE_URL, set to the version Vercel redirects away from.
Pick one primary domain, set the redirect once in Vercel, and remove registrar forwarding and code level host redirects that contradict it. Then make sure your site URL variable, auth callback URLs and canonical tags all use the primary domain. "How to connect a GoDaddy domain to Vercel" covers the DNS side if forwarding was set up at the registrar.
Could trailingSlash settings be the cause?
They can, when Next.js removes a trailing slash and another rule adds it back.
By default, Next.js redirects URLs with a trailing slash to the version without, so /about/ goes to /about. With trailingSlash set to true in next.config.js, it does the opposite and /about goes to /about/, except for files with extensions and paths under .well-known. A loop appears when something else disagrees:
- A redirect in next.config.js or vercel.json that adds or removes slashes in the opposite direction.
- Proxy code that normalises paths its own way.
- A CDN or proxy rule in front of Vercel that rewrites slashes.
- Links and canonical URLs written one way while the config prefers the other. That causes extra redirects, not usually a loop, but it wastes a hop on every visit.
Choose one style and let Next.js enforce it. If you truly need different behaviour for different paths, Next.js has a skipTrailingSlashRedirect option that turns off its automatic handling so your proxy can decide, but that is an advanced setting and easy to get wrong.
Is Cloudflare SSL Flexible mode the culprit?
If your domain's DNS is proxied through Cloudflare and the loop started when you turned the orange cloud on, it almost certainly is.
In Flexible mode, the browser talks to Cloudflare over HTTPS, but Cloudflare talks to your origin, here Vercel, over plain HTTP. Vercel always redirects HTTP to HTTPS, and Vercel's guide notes that this redirect cannot be turned off. So Vercel sends a redirect to HTTPS, Cloudflare passes it to the browser, the browser asks Cloudflare for HTTPS again, Cloudflare asks Vercel over HTTP again, and the loop never ends.
The fix is in the Cloudflare dashboard: open SSL/TLS for the domain and change the encryption mode from Flexible to Full (strict). Full (strict) needs a valid certificate on the origin, which Vercel issues for domains it has verified. Cloudflare's own troubleshooting page also lists Always Use HTTPS combined with an origin that redirects back to HTTP, and HSTS settings, as other causes worth checking.
If you do not need Cloudflare's proxy, setting the records to DNS only is the simplest option, and avoids two layers of redirects on every request.
A worked example
Here is an illustration with a made-up business. A tutoring company's booking app, built with an AI tool on Next.js and deployed to Vercel, starts showing ERR_TOO_MANY_REDIRECTS right after launch, but only for some visitors.
A private window loads the homepage fine but loops on /dashboard after sign-in. The Network tab shows /dashboard to /login to /dashboard, with no Cloudflare headers, so the loop is in the app. The proxy file checks for a session cookie and redirects to /login if it is missing. The login page has a client check that redirects signed in users to /dashboard.
Under Application, then Cookies, the session cookie exists for www.example.com, but the proxy is redirecting to the apex domain, because NEXT_PUBLIC_SITE_URL was set without www and the redirect uses it to build absolute URLs. On the apex domain the cookie does not exist, so the proxy sends the user to login, where Vercel's domain redirect sends them back to www, where the client check sends them to the dashboard.
The fix is to build redirects from the incoming request URL rather than a hardcoded site URL, set NEXT_PUBLIC_SITE_URL to the www version, and add a matcher so the proxy only runs on /dashboard and /account. Ten minutes of tracing, one small change.
When should you get help?
Get help when you have traced the chain and still cannot find the second rule, or when the loop involves authentication you do not fully understand.
Redirect loops are usually small fixes once found, which makes them a good fit for my Quick Fix, from $60 in about 2 days. If the loop is one of several launch problems, such as environment variables, auth callbacks and domains all misbehaving together, Fix Plus Features starts at $200 for up to three fixes plus one small feature. "Your Lovable or Bolt app works locally. Here is what breaks at launch." covers the wider launch checklist.
The bottom line
ERR_TOO_MANY_REDIRECTS in Next.js always means two rules disagree. Trace the Location headers in the Network tab or with curl, then test in a private window, on the vercel.app URL and locally to narrow it down. The usual causes are a proxy.ts (or middleware.ts) auth check that also runs on the login page, a session cookie that is not set or read where the decision is made, conflicting www and apex redirects, trailing slash rules fighting each other, and Cloudflare in Flexible SSL mode. Fix the rule that should not exist, keep one source of truth for each redirect, and the loop is gone for good.
Questions, answered in one line
What causes ERR_TOO_MANY_REDIRECTS on a Next.js site?
Two redirect rules that undo each other, such as an auth check that redirects to a login page it also protects, or a www rule and an apex rule pointing in opposite directions.
Is Next.js middleware still called middleware?
From Next.js 16 the middleware file convention is deprecated and renamed to proxy, so new projects use proxy.ts exporting a function named proxy.
Why does clearing cookies fix the redirect loop temporarily?
Because the loop depends on a stale or broken session cookie, and the fix is making sure the cookie is set and read consistently rather than clearing it every time.
How do I fix too many redirects with Cloudflare and Vercel?
In Cloudflare, open SSL/TLS settings for the domain and change the encryption mode from Flexible to Full (strict), then check for any remaining conflicting redirect rules.
Can Thomas Osayi fix a redirect loop on my AI-built app?
Yes, a single redirect loop fits the Quick Fix package, which starts at $60 and usually takes about 2 days.
Sources
- proxy.js file convention Next.js Docs
- trailingSlash Next.js Docs
- Deploying and redirecting domains Vercel Docs
- How do I resolve err_too_many_redirects when using a Cloudflare proxy with Vercel? Vercel
- ERR_TOO_MANY_REDIRECTS Cloudflare Docs
Related articles
- Supabase auth redirect URL not working? Fix localhost, preview and production redirects
- Environment variables undefined on Vercel? Every cause and fix
- How to fix a CORS error on a Next.js API route
- Firestore "Missing or insufficient permissions": what triggers it and how to fix it
Fix, finish and launch your AI-built app
Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.
See the service