Redirects in next.config.js not working on Netlify

I can’t share the URL with password, because it’s under NDA. The site is lagobe-lag.netlify.app.

Every redirect works fine in development and when built and served locally, but it doesn’t work when deployed to Netlify.

The app resides in a monorepo but each app deployed on Netlify has its own netlify.toml.
The documentation for next-runtime states that you need to set the full path in “publish” (which I guess is the “Publish directory”) when in a monorepo(*), and mine is set to that in the UI. If I try to set it in the netlify.toml then then it wont work, because it wants the path relative from the toml-file.

(*) GitHub - netlify/next-runtime: The Next.js Runtime allows Next.js to run on Netlify with zero configuration

The runtime-addon is installed and is showing up in the buildlog as:

9:38:21 PM: (build.command completed in 29.7s)
9:38:21 PM: ​
9:38:21 PM: @netlify/plugin-nextjs (onBuild event)                        
9:38:21 PM: 

What’s the redirect in question and how can we test this?

Thanks for your reply.
Sorry to be so secretive. As I said, we are subject to NDA.
Run atob('aHR0cHM6Ly9sYWdvYmUtbGFnLm5ldGxpZnkuYXBwL2FkdmljZS1mb3ItZmlyc3QtdGltZS1ib2FyZC1tZW1iZXJz')in your terminal to decode the URL.
The code is atob('ODczNQ==').
The URL should redirect to atob('aHR0cHM6Ly9sYWdvYmUtbGFnLm5ldGxpZnkuYXBwL3Jlc291cmNlcy9hZHZpY2UvYWR2aWNlLWZvci1maXJzdC10aW1lLWJvYXJkLW1lbWJlcnM=')

@hrishikesh Have you had a chance to look into this? I tried disabling password protection to see if that was the issue. Sadly it wasn’t.

Hey @kluplau,

I can see these 2 redirects in your config that are causing this issue:

I want to believe you’ve not added those, but I’m confirming it anyways. Have you added thsoe by any chance?

Thank you for your reply and you taking the time to investigate.

No I haven’t added those in redirects() in the Next config, nor Netlify.toml. Where else could this come from?

How did you get this information? What tool can I use?

If you’ve not added those, they must be coming from Next.js Runtime. In this case, you can simply add your custom redirects in netlify.toml like:

[[redirects]]
  from = "/other-language/*"
  to = "/other-language/404"
  status = 404

However, it would be useful to investigate where the redirect is coming from. The snippet I shared before was from our database. If it’s possible for you to share a minimal reproduction of your setup, we can investigate the source of that redirect.

Hi again @hrishikesh

I am affraid we aren’t 100% aligned on the issue now. The 404 I get is the error, I expected a redirect to a different path. I need to redirect domain.com/xxx to domain.com/yyy, in this case domain.com/old-slug should redirect to domain.com/resources/advice/old-slug. This is configured using Next’s redirects() function in the Next-config. The redirect works locally, but it returns 404 when deployed to Netlify.

I have invited you to a backup repo, so you can see the code. I invited Hrishikesh-K (Hrishikesh Kokate) · GitHub which I believe is you.

@hrishikesh Have you had a chance to look into this?

@hrishikesh Where can I get help, if not here? I need to launch the websites now.

My apologies, I mised your thread with some other one. I checked your repo now and I see what you mean. This looks like a bug right now on how we are generating the redirects. I’ll be filing it now and it will probably take some deeper investigation and time for it to be resolved. But the short-term solution I can offer you is to create a build plugin: Create Build Plugins | Netlify Docs and fetch the redirects from sanityClient.getRedirects() and then add it into Netlify config.

OR another thing you can try right now is to add a Netlify redirect as follows:

[[redirects]]
  from = "/*"
  to = "/.netlify/functions/___netlify-handler"
  status = 200

if that doesn’t work, you’d have to stick to creating a custom plugin for now.

The more I think about this, you can also move this logic to a Next.js middleware and it would work without you having to create a plugin.

I tried the [[redirects]] approach, and that didn’t work.

I can’t use the middleware-approach. All desired redirects are defined in the CMS, so I only want to query the CMS on build time. If I move the logic to the middleware (run-time) then the CMS will be queried too much, and I will extend my quota.

I will give the plugin approach a try now.

We’ve heard back from the devs and the cause has been identified to a platform limitation. There are 2 ways to solve it:

  1. Either use fallback: 'blocking' instead of fallback: false
  2. OR set the environment variable: LEGACY_FALLBACK_FALS to true. This would however cause a function invocation for every 404 on that dynamic route, which may have a cost implication.

Hi @hrishikesh. Thank you for your time.

The pages have been deployed and the plugin approach worked. I have marked that as the solution.

I might try the fallback approach, but I believe I did actually try that before consulting the forums.