In gatsby-node.js, export the createPages method and use the createRedirects action to generate any redirects that you want to add. Here’s an example showing the lasagna recipe above:
exports.createPages = async ({ graphql, actions }) => {
const { createRedirect } = actions
createRedirect({
fromPath: `/blog/recipes/mouthwatering-lasagna`,
toPath: `/recipes/mouthwatering-lasagna`,
})
}
In the example above, you’ve explicitly redirected one of your recipe urls, and after adding a few others, you realize that you won’t have time to get all the old urls. So you decide that any other url that uses your old path blog/recipes/* should just be redirected to the new /recipes path. Here’s how you’d handle that:
exports.createPages = async ({ graphql, actions }) => {
const { createRedirect } = actions
createRedirect({
fromPath: `/blog/recipes/mouthwatering-lasagna`,
toPath: `/recipes/mouthwatering-lasagna`,
})
// All your other redirects
createRedirect({
fromPath: `/blog/recipes/*`,
toPath: `/recipes`,
})
}