Can't seem to redirect /api/endpoint to /.netlify/functions/file/endpoint

I currently have a react website and react native app accessing some APIs through the following url

https://domain.com/api/endpoint
One of them is https://streamutt.com/api/latest_netflix

I started migrating this website into netlify and the only thing left to do is somehow redirect requests from /api/endpoint to /.netlify/functions/servers/endpoint (which I got working right now at https://streamutt.netlify.com/.netlify/functions/servers/latest_netflix)

This is the setting that I thought would make it work, but no matter what I change I wasn’t able to get it working at all.

[[redirects]]
   from = "/api/*"
   to = '/.netlify/functions/servers/:splat'
   status = 200
   query = {path = ":path"} #  apply this rule for /old-path?path=example

I’m also routing my calls through express and router() finishing it with

app.use('/.netlify/functions/servers', router )
module.exports = app;
module.exports.handler = serverless(app);`

Any help would be greatly appreciated

Hi, @ntoporcov.

This redirect is working because it has the query parameter:

https://streamutt.netlify.com/api/latest_netflix?path=foo

However, the API itself returns a 404 with this error:

Cannot GET /api/latest_netflix

This is the default 404 because it doesn’t redirect:

https://streamutt.netlify.com/api/latest_netflix

The redirect above only matches URLs with the query parameter in them.

I believe a second rule would fix this:

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

The same rule without the query parameter. Note, if there is more than one combination of query parameters, then each needs it’s own redirect. There is more about this in our documentation here. Quoting:

If you have multiple parameters, some of which are optional, this is the pattern to follow to ensure that we are prepared to handle all possibilities.

It also mentions putting the most general rules after the most specific.

Last but not least, I personally find the syntax of the _redirects file easier to work with. That is only my personal preference and it is definitely supported to define redirects in netlify.toml as well.

Would you try adding the redirect above? Also, please let us know if there are other questions.