Home > Software design >  Masking a URL destination file using Rewrite in Multi language config
Masking a URL destination file using Rewrite in Multi language config

Time:03-31

I'm trying to implement a rewrite that will mask the destination if it's '/dest' to '/new' The Next.js documentation suggested the following

module.exports = {
  async rewrites() {
    return [
      {
        source: '/dest',
        destination: '/new',
      },
    ]
  },
}

I'm having a hard time plugging it into my code which contains

 rewrites: async () => nextI18NextRewrites(localeSubpaths),
    publicRuntimeConfig: {
        localeSubpaths,
    },

CodePudding user response:

I believe I understand what the issue is. Multiple rewrites of one request does not work, it simple takes the first matching rewrite. In my case it was { source: '/:lang(en)/:path*', destination: '/:path*' }.

So it works if I add my rewrite above ...nextI18NextRewrites(localeSubpaths) while also manually adding the localeSubpath for it, e.g.

module.exports = {
  rewrites: async () => {
    return [
      {
        source: '/en/gardening/london',
        destination: '/services/gardening/london',
      },
      ...nextI18NextRewrites(localeSubpaths),
    ];
  },
  publicRuntimeConfig: {
    localeSubpaths,
  },
};
  • Related