如何在Vercel.json中实现特定URL带/不带尾斜杠的重定向?
解决Vercel中带/不带尾斜杠的URL重定向问题
要实现/docs/api/graphql/dam/overview/introduction和/docs/api/graphql/dam/overview/introduction/这两个路径的永久重定向,你可以简化原有的正则规则,直接匹配带或不带尾斜杠的目标路径:
正确的vercel.json配置
{ "redirects": [ { "source": "/docs/api/graphql/dam/overview/introduction/?", "destination": "/developers/docs/apis/asset-management/overview", "permanent": true } ] }
配置说明
source中的/?表示末尾的斜杠是可选匹配,这样就能同时命中带尾斜杠和不带尾斜杠的两个URL。- 如果你需要匹配
introduction后面还带任意子路径的情况(比如/introduction/subpage或/introduction/subpage/),可以把规则调整为:
这里的{ "source": "/docs/api/graphql/dam/overview/introduction(/.*)?", "destination": "/developers/docs/apis/asset-management/overview", "permanent": true }(/.*)?表示匹配可选的斜杠加任意后续路径,覆盖所有以introduction开头的子路径(带/不带尾斜杠)。
注意事项
- 确保规则放在
redirects数组中(Vercel的重定向配置需要包裹在这个数组内,原代码片段可能遗漏了外层结构)。 - 测试时可以用Vercel CLI的
vercel dev命令本地验证重定向是否生效,避免直接部署后出现问题。
内容的提问来源于stack exchange,提问作者Snivio
相关产品推荐
相关产品推荐

