# Error: Middleware redirected to a URL with a query string. Redirects must be to pathnames only.

- **ID:** `nextjs/middleware-redirect-to-internal-with-query`
- **Domain:** nextjs
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Next.js 13.x | active | — | — |
| Next.js 14.x | active | — | — |
| Next.js 15.x | active | — | — |

## Workarounds

1. **Redirect to a pathname without query strings, then handle query parameters client-side using useSearchParams or server-side in the target page.** (90% success)
   ```
   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.** (85% success)
   ```
   Use NextResponse.redirect with a path and then append query parameters via a server-side rewrite or client-side navigation.
   ```

## Dead Ends

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