Netlify redirects not working in production

In my _redirects, I have specified: /api/* /.netlify/functions/:splat 200!

I tried with/out the bang (!)

In development ,using netlify cli, the redirect works just fine. http://localhost:8888/api/latestTracks

In production, https://website.netlify.app/api/latestTracks leads to a 404.

Otherwise on the live site, when visiting, https://website.netlify.app/.netlify/functions/latestTracks I can see the JSON result just fine.

Hey @djmtype

Can you share your _redirects file?
Where is the _redirects file placed in the repository?
Does the deploy log show your redirects are processed e.g.

No redirects were processed. Is there a Gridsome-specific thing why it ignored my _redirects file?

_redirects file:

/api/* /.netlify/functions/:splat 200
/api/* https://gridsome-spotify-matrix.netlify.app/api/:splat 200

I added the 2nd line after to see if that would solve it. Nope.

Where have you placed the _redirects file? Is it in the static directory?

It also looks like a Gridsome directory thing as returned from Netlify Deploy.

A “_redirects” file is present in the repository but is missing in the publish directory “dist”.

Fair enough. I wrote it to the netlify.toml file instead:

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

I had originally placed the _redirects file on the same level as the functions directory and netlify.toml – at the root. Then I had tried placing it in the src directory, hoping when Netlify built the project, it would copy it over to dist. However, that didn’t work, and threw an error on the public facing site.

No, I did not trying placing it in the static directory.

As per Gridsome documentation

Files in this directory will be copied directly to dist during build

So in order for the _redirects file to get copied to dist for Netlify to process, you need to place it in the static directory in the project.

Thanks, and yes that does work too.

I found a downside to placing _redirects inside static directory.

When running netlify cli in development, _redirects can’t be found because it might be looking for it in the root?

http://localhost:8888/api/latestTracks results in a 404, but http://localhost:8888/.netlify/functions/latestTracks works.

Here’s my _redirects: /api/* /.netlify/functions/:splat 200

When using Gridsome, setting up redirects in netlify.toml seems like the easier option that works in both development and production.

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/:splat"
  status = 200

However, you might want a different redirects in production than in development.

Gridsome only copies static to dist during build, and seems to ignore when running in dev mode. Thus is appears using a netlify.toml a better option.

You can use both _redirects and [redirects] in a netlify.toml at the same time. Keep in mind that _redirects is processed first so if you have a redirect that starts with /* in _redirects no redirects in the netlify.toml will ever run.