#How to redirect to external url for some pages
1 messages · Page 1 of 1 (latest)
Hi @lavish juniper. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, you can accomplish this using rewrites in Next.js. You can define rewrites in your next.config.js file. Here's how you can set it up based on the information you provided:
module.exports = {
async rewrites() {
return [
{
source: '/blog',
destination: '<https://newblog/blog',>
},
{
source: '/blog/page/:page',
destination: '<https://newblog/blog/page/:page',>
},
{
source: '/blog/:slug',
destination: '<https://newblog/blog/:slug',>
},
]
},
}
In this configuration, the source is the incoming request path and the destination is where you want to redirect the request. The :page and :slug in the source are path parameters and they can be used in the destination.
Please note that if you're using trailingSlash: true, you also need to insert a trailing slash in the source parameter. If the destination server is also expecting a trailing slash it should be included in the destination parameter as well.
For more information, you can refer to the Next.js documentation on rewrites (https://nextjs.org/docs/api-reference/next.config.js/rewrites).
Sources:
- Next.js > Docs > Pages > Api-reference > Next-config-js > rewrites > Rewriting to an external URL (https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites#rewriting-to-an-external-url)
- Next.js > Docs > App > Api-reference > Next-config-js > rewrites > Rewriting to an external URL (https://nextjs.org/docs/app/api-reference/next-config-js/rewrites#rewriting-to-an-external-url)