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.

5 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.

3 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

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

1 Like

+1 for supporting context-dependent redirects!

In the meantime, @Pie 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.