Adding a 301 Redirect in Next.js

Tiempo de lectura: < 1 minuto

We will create a 301 redirect using next.js and in this case React.

White and Black Mountain - pexels

We will achieve that the old URLs can be redirected to the new ones, which is good for example to apply SEO.

For example, we will redirect a URL that is https://devcodelight.com/hello to https://devcodelight.com/goodbye

To do this, we will first go to next.config.mjs and add the following:

module.exports = { async redirects() { return [ { source: '/hello', destination: '/goodbye', permanent: true, // <--- 301 redirect }, ]; }, }; 

With this, we ensure that the URL redirection is clean for SEO.

Leave a Comment