javascriptnext.js

Rewrite rule not working in NextJS Config


I have tried to use rewrite rule of NextJS in next.config.js file to redirect urls '/lite' to '?amp=1' but it is not working in all scenerious.

Here is my next.config.js code.

async rewrites() {
    return [
      {
        source: '/:articles/:slug/:id/lite/',
        destination: '/:articles/:slug/:id/?amp=1',
      }
    ];
  },

}

module.exports = nextConfig

It is working in case of

https://localhost:3000/article/trending/us-man-witnesses-his-kidney-transplant-procedure-indian-origin-surgeon-viral-photos-9415947/

but not in case of

https://localhost:3000/article/trending/trending-globally/us-man-witnesses-his-kidney-transplant-procedure-indian-origin-surgeon-viral-photos-9415947/

Please help me on this.


Solution

  • I suggest you to update the given code to:

    async rewrites() {
        return [
          {
            source: '/:articles/:slug*/:id/lite/',
            destination: '/:articles/:slug*/:id/?amp=1',
          }
        ];
      },
    
    }
    module.exports = nextConfig

    Now this configuration works for both single and multiple segments (e.g., trending-globally) so it should work for both.