Issues with Conditional Redirects in netlify.toml and Next.js Runtime

Hello,

I’m currently facing an issue with setting up conditional redirects in my netlify.toml file. I have two domains, customdomain.com and maindomain.com, and I want to serve different content for each domain.

Here’s my current netlify.toml configuration:

[[redirects]]
  from = "/*"
  to = "/custompage/:splat"
  status = 200
  force = true
  conditions = {{ Host = ["customdomain.com"] }}

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
  force = true
  conditions = {{ Host = ["maindomain.com"] }}

With this configuration, I expect all requests to customdomain.com to be rewritten to /custompage/:splat and all requests to maindomain.com to be rewritten to /index.html.

However, I’m experiencing some unexpected behavior. When I check my deploy logs, I see the following message:

You have the following Netlify rewrites that might cause conflicts with the Next.js Runtime:


/* /custompage 200
/* /index.html 200

I’ve checked my next.config.js file and confirmed that there are no redirects or rewrites that could be conflicting with my Netlify rewrites.

Then on second attempt, I’ve applied this in netlify.toml lead no redirects (i.e. it renders maindomain.com):

[[redirects]]
  from = "https://www.customdomain.com"
  to   = "/custompage"
  status = 200
  force = true

error:

8:53:06 PM: You have the following Netlify rewrite that might cause conflicts with the Next.js Runtime:

8:53:06 PM:

8:53:06 PM: - https://customdomain.com /custompage 200

8:53:06 PM:

8:53:06 PM: For more information, see https://ntl.fyi/next-rewrites

On third attempt I’ve used the _redirects file

https://www.customdomain.com /custompage 200!

No luck either:

error:

7:59:55 PM: You have the following Netlify rewrite that might cause conflicts with the Next.js Runtime:

7:59:55 PM:

7:59:55 PM: - https://www.customdomain.com /custompage 200

7:59:55 PM:

7:59:55 PM: For more information, see https://ntl.fyi/next-rewrites

and no redirects (i.e. it renders maindomain.com)

I would appreciate any help or guidance on how to correctly set up these conditional redirects.
Thank you!

My site is https://localization-hq.netlify.app

The syntax is invalid - we do not support host in conditions.

Are you unable to create 2 different sites and add a custom domain to each individually?

@hrishikesh Many thanks for your comment. I’ve also tried approach 2 and 3 that I had added to my first question in this thread and these approaches seems to be in line with Netlify documentation (please see attempts 2 and 3). Still those did not work. Here they come again:

On second attempt, I’ve applied this in netlify.toml lead no redirects (i.e. it renders maindomain.com):

[[redirects]]
  from = "https://www.customdomain.com"
  to   = "/custompage"
  status = 200
  force = true

error:

8:53:06 PM: You have the following Netlify rewrite that might cause conflicts with the Next.js Runtime:

8:53:06 PM:

8:53:06 PM: - https://customdomain.com /custompage 200

8:53:06 PM:

8:53:06 PM: For more information, see https://ntl.fyi/next-rewrites

On third attempt I’ve used the _redirects file and again no redirect (i.e. it renders maindomain.com).

https://www.customdomain.com /custompage 200!

error:

7:59:55 PM: You have the following Netlify rewrite that might cause conflicts with the Next.js Runtime:

7:59:55 PM:

7:59:55 PM: - https://www.customdomain.com /custompage 200

7:59:55 PM:

7:59:55 PM: For more information, see https://ntl.fyi/next-rewrites

So, can you advise if the docs are wrong? Would that mean Netlify does not support domain redirects?

How can I achieve my intended redirect?

And to answer you question, no, I’d rather not have to create 2 identical sites for the sake of one page that has a different marketing copy.

Looking forward to your input!

Have you considered using Next.js middleware for this? As I understand, that is meant to handle exactly what you’re looking for, right?

Hi @hrishikesh thanks for commenting again, so I guess simple redirects don’t work for my use case? Netlify documentation and the Netlify bot do instruct the implementation I’ve done above.

And no I have not considered Next.js middleware, I expected this was supposed to be a simple Netlify redirect as per Netlify’s documentation.

You should be able to use it. I noticed you’re using an outdated version of Next.js Runtime. Have you tried upgrading it?

Hi @hrishikesh thanks for that nudge, but sadly updating Next hasn’t helped.

Just updated everything and the redirect still does not work, any new hints?

The only domain-based redirect you’ve added is for www.tableautranslation.com and that domain doesn’t resolve anywhere.

Solution for others in the future, the paths need to be full (contrary to what documentation states):

In other words, in your netlify.toml:

[[redirects]]
  from = "https://customdomain.com"
  to = "https://maindomain.com/custompage"
  status = 200
  force = true

thanks for sharing your solution with the community!