Nextjs 11, redirecting www to non www in a more general manner

I have a nextjs application where I want to redirect traffic from www requests to non-www.

I found this post, and used it to make a redirect that actually works:

    return [
      {
        source: '/:path*',
        has: [{ type: 'host', value: 'www.mywebsite.local' }],
        destination: 'http://mywebsite:3001/:path*',
        permanent: false,
      },
    ]

But, I need to deploy this to a test, and then production enviorment.
These enviorments have different domains (the test ones are prefixed with “test”)

Therefore, I would like to not have to write code that is specific for each enviorment with it’s specific domain name, but instead just redirect all www traffic to non-www.
No matter the domain name.

Can someone help me in doing that?

Leave a Comment