How do you use dynamic parameters within a redirect or rewrite rule to handle a whole pattern of URLs at once?
Short answer
You use a named parameter with a colon inside your source path, such as :slug, and reference that same parameter name inside your destination path, letting a single rule handle many different specific URLs that all share the same overall pattern.
Advertisement
AModel answer
You use a named parameter with a colon inside your source path, such as :slug, and reference that same parameter name inside your destination path, letting a single rule handle many different specific URLs that all share the same overall pattern.
// next.config.js
module.exports = {
async redirects() {
return [
{ source: '/blog/:slug', destination: '/articles/:slug', permanent: true }
];
}
};Real-world example
A publishing company renames its blog section to articles and sets up a single redirect rule using a dynamic slug parameter, automatically redirecting every existing blog post URL to its new equivalent address.
Likely follow-ups
- Can you combine multiple dynamic parameters in a single redirect rule?
- What happens if a requested path does not match the expected parameter pattern?
Related topics