#How to redirect to external url for some pages

1 messages · Page 1 of 1 (latest)

lavish juniper
#

I need to redirect to an external URL like: https://newblog/blog (no parameters) to these pages:

  • /blog
  • /blog/page/:page
  • /blog/:slug

How can i accomplish that? using rewrites?

radiant rainBOT
#

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:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: