Redirect with dynamic URL variables possible?

As we’re working on moving our website Netlify, I’m stumbling on the following issue:
We have a custom domain that we use for testing environments.
Each dev can create a testing environment and the url can be defined by them at build time. something like for example and the app at

These domains are not known in advance.

In our netlify site, we have a _redirects file that has something like this :

/login 301!

And the frontend links are relative, so the redirects will be environment agnostic.

Do the redirect rules support any type of dynamic URL structure so I could write something like this?

/login https://app-$

Or is there another way for me to support unknown in advance domains that point to the same netlify site?

@altryne, there isn’t a Netlify way to do this, but it should be possible to do this at build time with the build command itself.

How would you want to pass that environment variable to Netlify? As an environment variable configured in the UI?

If so, there can be a step in the build process (which could be a separate command/script) which dynamically generates a custom _redirects file and places it in the publish directory. The build itself will create (or add to) the _redirects file during the deploy to programmatically replace the environment variable with the literal string during the build.

Would this solution meet the requirements for this site?

Hey @luke , thanks for the quick response.
I already do this during build for 2 sites with same code (staging/prod)

The issue I have is, I want to point ALL of our custom environments to show the staging netlify site.
for example we might have
I want all of them to show the website, and all of them I want to point to Netlify

Let’s say we do this either via API and on build of each * site I add a domain alias in Netlify
The redirects in the staging environment will still have a hardcoded redirect URI, so a user on when they go to won’t arrive at which is the app in that environment, they will arrive at which is hardcoded in the URLs at time of build

Yup, as Luke said, we won’t be able to do that natively here - redirects are static at deploy time, and I don’t think you have a full list of hostnames. If I’m wrong and you do, feel free to make one redirect per name, like this: 301!

Otherwise you’d best be served by a function that can parse the original host header, or some other dynamic solution.

1 Like