如何在 Vercel 托管的 Gatsby 网站中进行 301 重定向?

How to make 301 redirect in Gatsby website hosted on Vercel?

image of vercel.json for reference

我正在为我的网站使用 gatsby js 并将其托管在 vercel 上,现在我想为我的网站进行 301 重定向,但我不想为此使用 gatsby-cloud。他们有什么方法我可以用 vercel 做到吗?

这是文档 https://vercel.com/docs/cli#project/redirects 但我无法正确理解它,因为它说要创建一个 vercel.json 文件,谁能帮助我更好地理解它?

当然,您不需要 Gatsby Cloud 进行重定向,您只需要服务器规则。

在 Vercel 的情况下,根据此 GitHub thread (and pulling a lof of similar threads), Next doesn't support by default Gatsby redirections of out the box so the only reliable approach is using createRedirect API。在您的 createPages 函数(如果存在)中添加以下内容:

// Generally you create redirects while creating pages.
exports.createPages = ({ graphql, actions }) => {
  const { createRedirect } = actions
  createRedirect({ fromPath: '/old-url', toPath: '/new-url', isPermanent: true })
  createRedirect({ fromPath: '/url', toPath: '/zn-CH/url', Language: 'zn' })
  createRedirect({ fromPath: '/not_so-pretty_url', toPath: '/pretty/url', statusCode: 200 })
  // Create pages here
}

Next 将使用通过此操作创建的重定向并创建适当的“现在”路由。