简体   繁体   English

路由 - 重定向(Next JS)

[英]Route - Redirection (Next JS)

I am moving my website (PHP) to Next JS but I have a question regarding routes.我正在将我的网站 (PHP) 移至 Next JS,但我对路线有疑问。

Currently I have this kind of routing (unique ID and slug from an API):目前我有这种路由(来自 API 的唯一 ID 和 slug):

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

And I manage some implementations, like:我管理一些实现,例如:

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

Can I do the same thing with Next JS or shall I use Express JS?我可以用 Next JS 做同样的事情还是应该使用 Express JS?

A feature like this is currently being discussed in this RFC on the Next.js GitHub. However, there is also a way to do this using the res.writeHead method in getServerSideProps此类功能目前正在 Next.js GitHub 上的此 RFC中进行讨论。但是,还有一种方法可以使用getServerSideProps中的res.writeHead方法来执行此操作

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

You could also redirect on the client side using the Router instance provided by next/router.您还可以使用 next/router 提供的 Router 实例在客户端重定向。

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

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