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
GET /home?preview=true will take me to

I tried with this rule
/* preview=true 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: Will show 404, because the page doesn’t exist. successfully take me to naturally takes me to an existing page as expected
But here’s my problem: 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 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 200!
/* 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 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:

1 Like