How do you set up a permanent redirect from an old URL to a new one in Next.js?
Short answer
You add a redirects function inside your next.config.js file, returning an array of redirect rules that each specify a source path, a destination path, and whether it should be a permanent redirect, which Next.js applies automatically without needing any additional code.
Advertisement
AModel answer
You add a redirects function inside your next.config.js file, returning an array of redirect rules that each specify a source path, a destination path, and whether it should be a permanent redirect, which Next.js applies automatically without needing any additional code.
// next.config.js
module.exports = {
async redirects() {
return [
{ source: '/old-blog', destination: '/blog', permanent: true }
];
}
};Real-world example
A company redesigns its website and adds a permanent redirect from its old blog URL to the new one, preserving search engine rankings that had built up over years at the old address.
Likely follow-ups
- What is the difference between a permanent and a temporary redirect?
- Do redirects configured in next.config.js apply during development as well as production?
Related topics