# 错误：中间件重定向到了包含查询字符串的 URL。重定向必须仅指向路径名。

- **ID:** `nextjs/middleware-redirect-to-internal-with-query`
- **领域:** nextjs
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

在 Next.js 中间件中，`redirect` 方法（来自 NextResponse）期望一个不带查询参数的路径名字符串。传递带有查询字符串的 URL 会导致内部错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Next.js 13.x | active | — | — |
| Next.js 14.x | active | — | — |
| Next.js 15.x | active | — | — |

## 解决方案

1. ```
   Redirect to a pathname without query strings, then handle query parameters client-side using useSearchParams or server-side in the target page.
   ```
2. ```
   Use NextResponse.redirect with a path and then append query parameters via a server-side rewrite or client-side navigation.
   ```

## 无效尝试

- **** — The redirect function does not support query strings; it strips them or throws an error. (80% 失败率)
- **** — NextResponse.redirect internally validates the URL; setting headers directly bypasses this but may cause hydration or caching issues. (50% 失败率)
- **** — Rewrite changes the URL server-side but does not cause a client-side redirect, which may not achieve the desired navigation. (60% 失败率)
