nextjs
config_error
ai_generated
true
Error: Middleware redirected to a URL with a query string. Redirects must be to pathnames only.
ID: nextjs/middleware-redirect-to-internal-with-query
85%Fix Rate
82%Confidence
1Evidence
2024-03-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Next.js 13.x | active | — | — | — |
| Next.js 14.x | active | — | — | — |
| Next.js 15.x | active | — | — | — |
Root Cause
In Next.js middleware, the `redirect` method (from NextResponse) expects a pathname string without query parameters. Passing a URL with a query string causes an internal error.
generic中文
在 Next.js 中间件中,`redirect` 方法(来自 NextResponse)期望一个不带查询参数的路径名字符串。传递带有查询字符串的 URL 会导致内部错误。
Official Documentation
https://nextjs.org/docs/app/building-your-application/routing/middleware#redirectingWorkarounds
-
90% success Redirect to a pathname without query strings, then handle query parameters client-side using useSearchParams or server-side in the target page.
Redirect to a pathname without query strings, then handle query parameters client-side using useSearchParams or server-side in the target page.
-
85% success Use NextResponse.redirect with a path and then append query parameters via a server-side rewrite or client-side navigation.
Use NextResponse.redirect with a path and then append query parameters via a server-side rewrite or client-side navigation.
中文步骤
Redirect to a pathname without query strings, then handle query parameters client-side using useSearchParams or server-side in the target page.
Use NextResponse.redirect with a path and then append query parameters via a server-side rewrite or client-side navigation.
Dead Ends
Common approaches that don't work:
-
80% fail
The redirect function does not support query strings; it strips them or throws an error.
-
50% fail
NextResponse.redirect internally validates the URL; setting headers directly bypasses this but may cause hydration or caching issues.
-
60% fail
Rewrite changes the URL server-side but does not cause a client-side redirect, which may not achieve the desired navigation.