There are several ways to handle custom redirects in NextJS app for Sitecore Headless.
1. Using redirects configurations defined on next.config.js file
We can use NextJS provided redirect functionality for this. We can either define these directly on the next.config.js file's "redirects" section OR can move that to a separate file and include it like below.
NOTE: Needs to include redirects.js file onto next.config.js to get the ...redirectsConfig
const redirectsConfig = require('./src/next-config/redirects');
2. Using Sitecore Redirect Template item
NOTE: Needs to include redirects.js file onto next.config.js to get the ...redirectsConfig
const redirectsConfig = require('./src/next-config/redirects');
We wanted a way to provide a method to add redirects in case editors wanted to add that url into dynamically generated menus on the site.
So we decided to take the custom Redirect App Route template with RedirectURL field in it.
This called handling redirects from server side in NextJS app.
Code will look like something below: