Next.js · Junior · Question 56 of 75

How do you redirect the old /blog/:slug URLs to /news/:slug for every request, without code in pages?

// next.config.ts
async redirects() {
  return [{ source: '/blog/:slug', destination: '/news/:slug', permanent: true }]
}

Explanation

Config redirects run before the filesystem routes are matched. For many dynamic redirects (e.g. from a database), use proxy.ts instead.

Further reading

Problem with a question

Next.js · Junior · question 56

Test yourself

Take the Next.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz