Multiple rules in _redirects file not working

  • I made application which has client which is hosted on netlify and server which is hosted on heroku .

  • to connect client with server i have included rule (i.e /* https://my_app.herokuapp.com/api/:splat 200) in _redirects file at the top .

  • after that I wrote general rule /* /index.html 200 this is working perfectly fine (i.e. I can use my application as i expected) .

  • But when i refresh page it shows Cannot GET /api/profile

  • I changed the order of rules of _redirects than I made observation that only first rule is working(i.e. when i wrote rule /* /index.html 200 at the top then there is no issue in refreshing but this time my client is not able to fetch data from server.)

  • It is showing two rules are processed when i deployed client on netlify.

PLEASE HELP ME :pray:

Hi @Vaibhav

The issue here is there are two catchall/wildcard (/*) rules. If you have

/* https://my_app.herokuapp.com/api/:splat 200
/* /index.html  200

everything get routed to heroku and nothing to the app. Conversely, if you have

/* /index.html  200
/* https://my_app.herokuapp.com/api/:splat 200

everything gets routed to the app and nothing to heroku.

The best solution here is to have a specific /api rewrite e.g.

/api/* https://my_app.herokuapp.com/api/:splat 200
/* /index.html  200

so only /api/profile or /api/something route through to heroku.

Thanks @coelmay I understand what you explained but this time server is not connected to client properly.

  • Taking reference through documentation I also tried,
    https://client.netlify.app/* https://server.herokuapp.com/api/:splat 200 /* /index.html 200 but in this also /* /index.html 200 this rule is not working (i.e. when i refersh the page it says Cannot GET /api/something).

Can you provide another solution or any suggestion please.

You cannot have two rules for /* @Vaibhav, no matter which way they are formatted.

This rule is the same as
/* https://server.herokuapp.com/api/:splat 200

The solution is provided previously

/api/* https://my_app.herokuapp.com/api/:splat 200
/* /index.html  200

Thank you so much @coelmay :innocent:

  • It worked. I used multiple rules for different routes in _redirects.

  • The rules are like:
    /user/* https://server.herokuapp.com/api/user/:splat 200,
    …followed by different routes
    /* /index.html 200.

Great you found a solution @Vaibhav.

If you had have used the solution I offered (i.e. /api/* ...) all calls to the back-end would go through this one redirect e.g.

/api/users, /api/login, /api/profile, and so on. That is what the * and the :splat are doing.

1 Like

Yeah, I got the logic behind the solution you offered but my client side uses http://client.netlify.app/user url to connect with server. It is not like https://client.netlify.app/api/user thats why i used multiple rules.