繁体   English   中英

路由 - 重定向(Next JS)

[英]Route - Redirection (Next JS)

我正在将我的网站 (PHP) 移至 Next JS,但我对路线有疑问。

目前我有这种路由(来自 API 的唯一 ID 和 slug):

www.website.com/section/id_slug-of-post

我管理一些实现,例如:

www.website.com/section/id => 301 to www.website.com/section/id_slug-of-post

www.website.com/section/id_slug-with-wrong-words => 301 to www.website.com/section/id_slug-of-post

www.website.com/section/wrong-id => 301 to www.website.com/section

我可以用 Next JS 做同样的事情还是应该使用 Express JS?

此类功能目前正在 Next.js GitHub 上的此 RFC中进行讨论。但是,还有一种方法可以使用getServerSideProps中的res.writeHead方法来执行此操作

export const getServerSideProps = (ctx) => {
    ctx.res.writeHead(301, { Location: '/new/url' });
}

您还可以使用 next/router 提供的 Router 实例在客户端重定向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM