Redirect to external url if param is present

Hi! I’d like to setup netlify in a way that, when a param is present, it should take me to the preview server.

Like this:
GET /home Will take me to the normal homepage
but
GET /home?preview=true will take me to https://www.test.com/home

I tried with this rule
/* preview=true https://www.test.com/:splat 200

And it kinda works.

If I visit any non-existing page, it does the redirection correctly and as expected.
But if the route matches a page that actually exists, it won’t redirect, just show the file.

Here are the examples:
https://distracted-jepsen-ca33fe.netlify.com/hello Will show 404, because the page doesn’t exist.
https://distracted-jepsen-ca33fe.netlify.com/hello?preview=truewill successfully take me to www.test.com/hello
https://distracted-jepsen-ca33fe.netlify.com/test naturally takes me to an existing page as expected
But here’s my problem:
https://distracted-jepsen-ca33fe.netlify.com/test?preview=true Still takes me to the existing page, doesn’t do the redirect.

What am I doing wrong?

Oh! Just found the problem. I needed to add 200! to my rule instead of just 200

Good catch! Also I am not sure your formatting is quite right - I think you should be using /* preview=:preview https://www.test.com/:splat 200! for best results. We don’t have any way of evaluating the VALUE of the parameter while choosing to proxy or not, just the PRESENCE of the parameter.

Awesome, I’ll try that. Thanks!

Also, is there a way to proxy to one address if there’s a param, but to another if there isn’t?

Like combining these 2 rules:

/* preview=:preview https://www.staging.test.com/:splat 200!
/* https://www.test.com/:splat 200

I guess my question is: if there is no parameter, would the first rule be ignored?

Yup, that will work as you’re expecting (however without the “!” on the end of the second 200, it will only work for FILES NOT PART OF YOUR DEPLOY ALREADY!

See https://www.netlify.com/docs/redirects/#note-on-shadowing for more details about what the “!” means and why if you want it on one of those rules…you probably want it on both :slight_smile: