Redirect in _redirects based on relative url

We have multiple env like alpha, beta, preprod and prod. Current in _redirect we have redirects like
/test https://alpha.pi.com 301

So now www.pi.com/test will redirect to https://alpha.pi.com. Since the destination is absolute url, if we create multiple env all the env redirect only to https://alpha.pi.com. Is there any way to handle it in _redirects.

@Ramji_R There is no “conditional logic” in the _redirects

But you could solve for this in your build system by generating the _redirects file based on what you’re building for deployment.

If you didn’t want to generate the _redirects file you could also just maintain multiple of them, and copy the appropriate one into the Publish directory at the point of deployment.

The _redirects file is just plain text, so you could easily generate it however you’re most comfortable.

@nathanmartin Thanks for the suggestions. can you share netlify docs on how to achieve both the suggestion i.e (Generating _redirect on deployment, how to map appropriate _redirect to the publish directory on deployment)

@Ramji_R No, because I don’t work for Netlify, and there is no such documention.

What I’ve proposed is just regular development.

You can create the file however you wish, with whatever system/language you’re most comfortable, the output just has to be in the format of the _redirects file. If you’re working on a project that has four different environments I’d imagine you have someone on the team that can dynamically create a text file.

In regards to “copying the file”, I just mean creating multiple redirects somewhere (it could be anywhere), for example:

redirects/
  alpha
  beta
  preprod
  prod

Then simply copy the “one that is applicable to the currently running build” into your Publish directory, you could do this in the build command with something as simple as:

cp redirects/alpha dist/_redirects