简体   繁体   中英

Route - Redirection (Next JS)

I am moving my website (PHP) to Next JS but I have a question regarding routes.

Currently I have this kind of routing (unique ID and slug from an API):

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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