用于重定向无效路由的正确状态代码

proper status code for redirecting an invalid route

我设置了以下路由:

/business //look for a business
/business/biz/:id //look at a specific business

我所做的是,如果有人去 /business/biz,它会重定向到 /business。 我的问题是我应该为此使用什么状态代码? 我不认为它是 301302,因为它不是永久性的移动,也不是临时的,它只是一个页面,不存在一致的重定向到特定页面。

//get /business/biz page
router.get("/biz", (req, res) => {
  res.redirect("/business");
});

我在 Stack Overflow 上查看了不同的问题,但没有找到符合我的案例。

要在 /business 上重定向 /business/biz,您应该 return 一个 308 Permanent Redirect
这是维基百科关于状态 308 的引用:

The request and all future requests should be repeated using another URI. 307 and 308 parallel the behaviors of 302 and 301, but do not allow the HTTP method to change. So, for example, submitting a form to a permanently redirected resource may continue smoothly

您还应该检查 解释 301308302307 之间的差异,并且写得很好。

建议仅将 301 代码用作 GETHEAD 方法的响应,并使用 308 永久重定向 POST 方法。

查看全文here