# 错误：URL 无效。中间件重写目标必须是相对路径或内部 URL。

- **ID:** `nextjs/middleware-rewrite-absolute-url`
- **领域:** nextjs
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

中间件使用 NextResponse.rewrite() 时传入了绝对外部 URL，而非相对路径或内部路由。Next.js 中间件重写仅限于同源路径。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 14.2.0 | active | — | — |
| 15.0.0 | active | — | — |
| 14.2.15 | active | — | — |

## 解决方案

1. ```
   Change the rewrite target to a relative path. If you need to proxy an external URL, create an API route that fetches the external resource and returns it.
   ```
2. ```
   Use NextResponse.next() and modify the response headers to forward the request to an external service via a reverse proxy setup (e.g., Nginx or Vercel rewrites in vercel.json).
   ```

## 无效尝试

- **Adding trailing slash or encoding the URL** — The issue is the domain mismatch, not URL formatting. Trailing slashes or encoding don't change the absolute external nature. (80% 失败率)
- **Using NextResponse.redirect() instead of rewrite()** — Redirect also expects a relative path or internal URL in this context; it's not a rewrite-specific restriction. (70% 失败率)
- **Setting headers to allow external redirects** — Middleware rewrites are fundamentally disallowed for external URLs regardless of headers. The restriction is baked into Next.js's routing logic. (90% 失败率)
