I currently have 3 different branches in Github (dev/test/prod) and 3 API servers (dev/test/prod) on non-Netlify domains, and I am currently using the Netlify _redirects file to handle redirection of API calls to these domains. For example, dev would have a redirects file like:
My problem is when I create pull requests from one environment to another (e.g. Dev to Test) - these _redirect files get overwritten in the pull request (e.g. /api/* for test starts pointing to dev server) and I have to manually rewrite these files after accepting the pull request.
I would avoid committing the _redirects file in Git, but then Netlify wouldnāt be able to read it. Alternatively, I would use different contexts in netlify.toml to have my redirect rules vary for each environment, but redirects seem to be a special case and canāt be set per-context in this file.
Is there any way I can avoid manually rewriting the _redirects file for each branch merge? I only need to set these very infrequently, but I merge branches often.
We have an open feature request to support putting redirects in deploy context, Iāve added your voice to this request so weāll let you know once itās been added.
What most people do in this situation is to write a script, could be in any language or just do it in bash. This script checks the BRANCH or CONTEXT and depending on what is there, it renames a file. This way you prepare multiple _redirects files with different names, and rename the one you want to be active to _redirects. You can also dynamically generate a _redirects file inside of a script during your build process if you prefer doing it that way.
No changes in status, we are very good about posting in feature request threads when updates occur. You can also watch the Updates category if you donāt trust us to follow up here
This feature would be amazing. Even if we could have env variables, similar to signed proxy requests like:
[build.environment]
API_URL = "https://dev.my-api-instance.amazonaws.com"
[context.production.environment]
API_URL = "https://live.my-api-instance.amazonaws.com"
[[redirects]]
from = "/api/*"
to = ":API_URL/:splat"
status = 200
or yet
[[redirects]]
from = "/api/*"
to = "https://test.my-api-instance.amazonaws.com/:splat"
status = 200
[[context.production.redirects]]
from = "/api/*"
to = "https://live.my-api-instance.amazonaws.com/:splat"
status = 200
Although I donāt have a specific example, what you can do is make use of the $CONTEXT environment variable and pass it through to a script, as detailed here:
As a very small example workflow, we could āpass onā the $CONTEXT variable into a file that you can access at browse time, like this, during build:
npm run build && echo $CONTEXT > public/netlify-context.txt
In theory, you could:
Have different _redirects files, based on contexts (i.e. _redirects-prod, _redirects-prev)
In your bash script, look for a specific context and move the respective _redirects-[context] file to _redirects
+1 for this concept!! Iām using role based redirects with Netlify Identity and currently commenting out the redirects locally when I run Netlify Devā¦ I know Iāll screw up though doing this!! Is there a way to disable the redirects on Netlify Dev or run a different netlify.toml? Not sure I can use _redirects with role based redirects??
Any news ? You be amazing.
I migrate old project to new language.
So i got some redirect old page ā new page into netlify.toml.
But itās only in production so my staging also redirect to production. :-S