Rewrite only works with netlify dev, not in deployed environment

Hi all, I’m struggling to understand why my rewrite isn’t working.

Here is my netlify.toml:

[[redirects]]

  from = "/js/script.js"
  to = "https://plausible.io/js/plausible.js"
  status = 200

[[redirects]]

  from = "/api/event"
  to = "https://plausible.io/api/event"
  status = 200

The first (/js/script.jshttps://plausible.io/js/plausible.js) works great. The second gets a 404 response (not Plausible’s 404 page or the 404 page for my site ). Here’s my site: https://deepmay.net/ you can see event in the network tab 404.

However, when running netlify dev locally, the route is proxied just fine from localhost:8888/api/event to https://plausible.io/api/event

I have the exact same setup (gatsby + plausible + proxy) running fine in Vercel, so I don’t think it’s a Gatsby issue (at first I thought maybe the /api route was getting tangled up with some gatsby functions route logic, but that doesn’t seem to be the case)

hi there, what files exist at the "https://plausible.io/api/event" route? is there an index page in that directory?

Sorry, i should have given more context:
Plausible is a third party analytics tool. https://plausible.io/api/event is an endpoint that their js snippet POSTs events to.

There are no files located at /api/event in my project. I just tried force = true on this and get my site’s 404 page as a response instead of the empty ‘Not found’ 404 response. Here’s a preview with force = true https://deploy-preview-130--deepmay.netlify.app/

Hey there, @chip-drop :wave:

Thanks for your patience here. Can you try moving

/api/event https://plausible.io/api/event 200!

to the top of your _redirects file? My assumption is that re-ordering the redirects in the order that you want you execute them will address this. Let me know if that works! If it doesn’t, I can bring this to one of our teams to investigate further.

1 Like

Reordering the redirects in netlify.toml didn’t work, but moving all of the redirects to a _redirects file (and reordering them) did! Wish I understood why, but I’m happy enough now that it works. Thanks a ton for the suggestion!

1 Like