Using different redirects across different contexts/environments

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

Hiya @MeWhit - No update is intended; we explained how you can configure things in a post above: [Support Guide] Conditional build/deploy behavior for context / branch on Netlify

Did you have some trouble implementing that?

@fool Thanks for sharing! The article talks about context-specific configurations in netlify.toml, but I see here that this does not apply to redirects. Are you saying this is actually possible?

Certain keys, such as [build] and [[plugins]] but not [[redirects]] or [[headers]] , allow you to set [context] properties based on the kind of deploy.

The recommended way is outlined in the docs: Custom headers | Netlify Docs