Using different redirects across different contexts/environments

Hi all,

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:

/api/*         http://dev.my-api-instance.amazonaws.com/:splat          200

And Test would have a file like

/api/*         http://test.my-api-instance.amazonaws.com/:splat          200

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.

7 Likes

I was able to work around this by creating an additional branch for the _redirects file change. For example, if I was promoting work from Dev to Test:

  1. Create a new branch from Dev (e.g. ā€˜devtotestā€™)
  2. Update the _redirects file with the Test API server location in devtotest branch
  3. Pull request from devtotest to Test branch

Not great, and somewhat error prone, but at least iā€™m not changing live pages as I go. Still interested in a better approach.

Hi @dvasdekis,

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.

4 Likes

Thanks Gerald. Any way I can keep track of this feature request, so I know when I can switch?

@dvasdekis, the feature request isnā€™t publicly accessible. This community topic and the feature request are ā€œcross-linkedā€ though.

We will definitely follow-up here in community if/when the feature is added to let you (and anyone else searching here) know about it.

Iā€™d like to add a +1 for this feature request. Thanks!

4 Likes

Gentle nudge from me - any news? Thank you :slight_smile:

It is still an open feature request, @dvasdekis. :slight_smile:

Hi guys - happy issue birthday! Any news? Thank you :slight_smile:

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 :wink:

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
1 Like

Understood, @satoshi.

It can be achieved using bash scripts though I appreciate that enhancing our _redirects/netlify.toml would be nice!

1 Like

@Scott, do you have an example of how would go about doing this?

1 Like

+1 for supporting context-dependent redirects!

In the meantime, @Scott Iā€™d also appreciate an example of how to solve this with bash scripts. Thanks in advance!

@taras, @samajammin,

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
  • Enjoy your sweet, sweet context-derived redirects :muscle:
2 Likes

There is also a good write-up of that pattern, with a different example here:

1 Like

Push from me. This is very important.

+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??

You can. Hereā€™s an example in the docs

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