错误:重写目标 '/X' 未找到。目标路径必须是有效的路由。
Error: Rewrite destination '/X' not found. The destination path must be a valid route.
ID: nextjs/rewrite-destination-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Next.js 13.4 | active | — | — | — |
| Next.js 14.0 | active | — | — | — |
| Next.js 14.2 | active | — | — | — |
| Next.js 15.0 | active | — | — | — |
根因分析
next.config.js 中的重写规则指向一个在应用程序中不存在或不是有效内部路径的目标路由(例如,缺少文件、路径拼写错误)。
English
A rewrite rule in next.config.js points to a destination route that does not exist in the application or is not a valid internal path (e.g., missing file, typo in path).
官方文档
https://nextjs.org/docs/app/api-reference/next-config-js/rewrites解决方案
-
在目标路径创建缺失的路由文件。例如,如果目标是 '/blog/[slug]',则创建 'app/blog/[slug]/page.tsx'。
-
更正目标路径以匹配现有路由。检查 'app' 或 'pages' 目录中的文件结构,并相应更新 next.config.js。
-
如果目标是外部 URL 或不存在的内部路径且应指向其他地方,则使用重定向代替重写。
无效尝试
常见但无效的做法:
-
80% 失败
Trailing slashes do not create a route; the destination must exist as a file or directory. The error persists because the underlying route is still missing.
-
70% 失败
The error is specifically about the destination, not the source. Modifying the source may change behavior but does not resolve the missing destination.
-
95% 失败
Rewrites are for internal routes; external URLs require redirects. The error explicitly states the destination must be a valid internal route.