Skip to content
On this page

Vercel 重定向


标签:vitepress/deploy  

我的笔记分享是目前通过 Vercel 部署的 Vitepress 应用。

默认情况下,vercel 提供静态页面服务只能关闭 vitepress 的 cleanUrls 功能。

如果开启 cleanUrls 的功能,虽然在使用的时候没有问题(SPA内部跳转),但拷贝页面链接分享给别人,或者别人保存进收藏栏再打开就是 404 了。

以前遇到类似的情况,是在 404 页面重新进行跳转(页面可能会闪烁一下),不过那时候还是使用 Github Pages。现在改用 Vercel,提供了更强大的配置功能。

详见文档:Rewrites on Vercel | Vercel Docs

Vitepress 配置 cleanUrls:Site Config | VitePress

比如我的笔记页面都是在 /note 目录下的,所以我这样写vercel.json

json
{
  "rewrites": [
    {
      "source": "/note/:path",
      "destination": "/note/:path.html"
    }
  ]
}

注意两个别写反了,是把没有 html 的重定向到 html,不然没效果,别问我怎么知道的

Last updated: