Redirects randomly give a 404 (Next.js)

I have next.js app with two domains. The second domain redirects to a sub-path of the first one. Navigating to the second domain or refreshing the page gives randomly a 404 error, especially with a clear cache (e.g., a new incognito tab).

You can test the randomly breaking redirect here: https://test2.map.avoin.org/
The netlify address is https://hiilikartta-deploy-test–avoin-map-test.netlify.app/

The build is using Next.js 14.2.3 and the V5 Next.js runtime.

Here is my netlify.toml

[[redirects]]
from = "https://test2.map.avoin.org/_next/*"
to = "https://test.map.avoin.org/_next/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/adds/*"
to = "https://test.map.avoin.org/adds/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/api/*"
to = "https://test.map.avoin.org/api/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org"
to = "https://test.map.avoin.org/hiilikartta"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/*"
to = "https://test.map.avoin.org/hiilikartta/:splat"
status = 200
force = true

Thanks for any help or insights!

The problem is because you’re using a custom domain in the redirect path. Try changing the redirects to (all redirects):

[[redirects]]
from = "https://test2.map.avoin.org"
to = "https://avoin-map-test.netlify.app/hiilikartta"
status = 200
force = true

Thank you for the response! However, now the redirects always give a 404.

[[redirects]]
from = "https://test2.map.avoin.org/_next/*"
to = "https://avoin-map-test.netlify.app/_next/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/adds/*"
to = "https://avoin-map-test.netlify.app/adds/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/api/*"
to = "https://avoin-map-test.netlify.app/api/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/hiilikartta"
to = "https://avoin-map-test.netlify.app/hiilikartta"
status = 200
force = true


[[redirects]]
from = "https://test2.map.avoin.org/*"
to = "https://avoin-map-test.netlify.app/hiilikartta/:splat"
status = 200
force = true

If I use the branch deploy URL in combination with the test.map.avoin.org domain it kinda works:

[[redirects]]
from = "https://test2.map.avoin.org/_next/*"
to = "https://hiilikartta-deploy-test--avoin-map-test.netlify.app/_next/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/adds/*"
to = "https://hiilikartta-deploy-test--avoin-map-test.netlify.app/adds/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/api/*"
to = "https://hiilikartta-deploy-test--avoin-map-test.netlify.app/api/:splat"
status = 200
force = true

[[redirects]]
from = "https://test2.map.avoin.org/hiilikartta"
to = "https://test.map.avoin.org/hiilikartta"
status = 200
force = true


[[redirects]]
from = "https://test2.map.avoin.org/*"
to = "https://test.map.avoin.org/hiilikartta/:splat"
status = 200
force = true

Interestingly, the website breaks if I replace the last redirect with this:

[[redirects]]
from = "https://test2.map.avoin.org/*"
to = "https://hiilikartta-deploy-test--avoin-map-test.netlify.app/hiilikartta/:splat"
status = 200
force = true

So any sub-path of /hiilikartta will give a 404. There is clearly something wrong with something. And I would rather not have to use the branch domain. Any ideas? Thanks!

Yes, because now they’re consistently being matched correctly.

You can use any netlify.app URL. Using custom domains in rewrites is not recommended.

Do you have a link to a deploy with these attempts:

Sure. I made the first one the production deploy. Here is also a permalink: https://663e060580266700081992d6–avoin-map-test.netlify.app/
And here is the code: https://github.com/AvoinOrg/climate-map/blob/a6978fcf8e18baba95dd8ee9d21b7039bf7b1b95/netlify.toml

Permalink to the second configuration: https://6640ea0344698f0008facb78–avoin-map-test.netlify.app/
Code: https://github.com/AvoinOrg/climate-map/blob/ca3e4849f8d965b3b87f0f54c1e0abc08f425bc4/netlify.toml

Permalink to the third one: https://6640eb111962b50008940bb0–avoin-map-test.netlify.app/
Code: https://github.com/AvoinOrg/climate-map/blob/7b8590ac398b41bd3acab1d3994e2f74d98349bf/netlify.toml

Hi there!

Thanks for following up, and my sincere apologies for the delay! Can you let us know if you’re still running into this issue? In reviewing/refreshing the above permalinks, I’m unable to currently replicate any 404 errors at the above routes, so any live examples will help our team dig back in.