# 错误：中间件重写后查询参数丢失

- **ID:** `nextjs/rewrite-query-params-lost`
- **领域:** nextjs
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

在中间件中使用 NextResponse.rewrite() 时，如果目标 URL 已有自己的查询字符串或目标是静态路径，则查询参数会被剥离。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| next@14.1.0 | active | — | — |
| next@14.2.0 | active | — | — |
| next@15.0.0 | active | — | — |

## 解决方案

1. ```
   使用 NextResponse.next() 并修改 URL 包含查询参数，然后在页面组件中通过 searchParams 属性处理重写。
   ```
2. ```
   在重写前将查询参数存储在 cookie 或 header 中，然后在页面组件中读取。
   ```

## 无效尝试

- **** — Next.js normalizes the URL and may strip duplicate or malformed query strings, especially if the destination path is a file-based route. (70% 失败率)
- **** — Redirect changes the URL in the browser, which may break client-side state or cause infinite loops if the redirect target is the same page. (80% 失败率)
- **** — The request object is immutable in middleware; modifying it directly has no effect on the rewrite. (90% 失败率)
