# Error: Rewrite query parameters are lost after redirect in middleware

- **ID:** `nextjs/rewrite-query-params-lost`
- **Domain:** nextjs
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

When using NextResponse.rewrite() in middleware with a URL that includes query parameters, the parameters are stripped if the destination URL already has its own query string or if the rewrite target is a static path.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| next@14.1.0 | active | — | — |
| next@14.2.0 | active | — | — |
| next@15.0.0 | active | — | — |

## Workarounds

1. **Use NextResponse.next() with a modified URL that includes query parameters, then handle the rewrite in the page component using the searchParams prop.** (85% success)
   ```
   Use NextResponse.next() with a modified URL that includes query parameters, then handle the rewrite in the page component using the searchParams prop.
   ```
2. **Store the query parameters in a cookie or header before rewriting, then read them in the page component.** (75% success)
   ```
   Store the query parameters in a cookie or header before rewriting, then read them in the page component.
   ```

## Dead Ends

- **** — Next.js normalizes the URL and may strip duplicate or malformed query strings, especially if the destination path is a file-based route. (70% fail)
- **** — 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% fail)
- **** — The request object is immutable in middleware; modifying it directly has no effect on the rewrite. (90% fail)
