# 错误：无效的重写目标。重写目标必须是相对路径或同一应用程序内的绝对 URL。

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

## 根因

next.config.js 中的重写规则有一个指向外部 URL（例如 https://other-site.com）或无效路径的 `destination`，但 Next.js 中的重写只允许重定向到同一应用程序内的内部路由。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Next.js 13.0.0 | active | — | — |
| Next.js 14.0.0 | active | — | — |
| Next.js 14.2.0 | active | — | — |

## 解决方案

1. ```
   将目标更改为应用程序内的相对路径。示例：`rewrites: async () => [{ source: '/old-path', destination: '/new-path' }]`。如果需要代理外部 API，请改用 API 路由。
   ```
2. ```
   如果需要代理外部 API，请创建一个从外部 URL 获取数据并返回响应的 API 路由。示例：在 app/api/proxy/route.ts 中，使用 `fetch('https://other-site.com/api/data')` 并返回结果。
   ```
3. ```
   使用中间件通过返回包含外部内容的 Response 对象来重写请求到外部 URL。示例：在 middleware.ts 中，`return fetch('https://other-site.com' + request.nextUrl.pathname)`。
   ```

## 无效尝试

- **** — The destination is still external; Next.js does not allow rewrites to external URLs regardless of formatting. (100% 失败率)
- **** — Redirects to external URLs are allowed, but if the intent was to proxy content (rewrite), this changes the URL in the browser, which may not be desired. (50% 失败率)
- **** — The basePath option only affects the application's own URL prefix, not rewrite destinations; the error will persist. (95% 失败率)
