ERR_TOO_MANY_REDIRECTS nextjs routing_error ai_generated true

Error: ERR_TOO_MANY_REDIRECTS / redirect loop in middleware

ID: nextjs/middleware-redirect-loop

Also available as: JSON · Markdown
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
14 active

Root Cause

Middleware redirects to a URL that triggers the middleware again, causing infinite loop.

generic

Workarounds

  1. 95% success Add matcher config to exclude the redirect destination from middleware
    export const config = { matcher: ['/((?!login|_next|api|static).*)'] };

    Sources: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher

  2. 92% success Check for the redirect destination in the condition: if (url !== '/login') redirect('/login')
    if (url !== '/login') redirect('/login')

    Sources: https://nextjs.org/docs/app/building-your-application/routing/middleware

Dead Ends

Common approaches that don't work:

  1. Add a redirect counter in cookies 75% fail

    Over-engineering — fix the matcher config instead

  2. Disable the middleware temporarily 70% fail

    Doesn't fix the issue — it'll come back when re-enabled

Error Chain

Frequently confused with: