Complex Redirect with Query Params

Hi i am wondering if this is possible with redirects. These are all example urls

User lands at “/netlify/gatsby” it redirects to “netlify?tool=gatsby” (this works as intended)
User lands at “/netlify/gatsby?campaign=2022” it redirects to “netlify?tool=gatsby&campaign=2002” (i can not get this working no matter what i try)

We wont always know that the query parms are campaign=x so need that to be flexible to work with any query params that the user arrives with

Hi @isaac-martin,

We saw your helpdesk ticket too, but I personally chose forums to reply to this so more people can benefit from this discussion. Let us know if that’s not what you’d want and we can go back to the helpdesk.

To assist you with this, would you let us know what behaviour exactly you’re looking for? I undrstood this line:

But, I’m not sure what the expected behaviour is here:

Looks like you’ve stated the problem, but I don’t know how you want the redirects to behave there. So if you could add more insights to that, we can advise accordingly.

we use the value from the tool=gatsby param to pass into a client side api request as a variable

Imagine we have some kind of hero content that says - Welcome Gatsby User" or “Welcome Next User” - our use case is more complex than that and it is >1000 params that could be passed into “tool=”

Maybe a better way of writing the second line is below. The first part of the url doesn’t really matter. I’ll attach a similar version of the redirect rule i have tried below to see if that helps visualize the problem.

User lands at “/netlify/gatsby?campaign=2022” it redirects to “/special?tool=gatsby&campaign=2002”

const tools = ["gatsby","next", ...] // length of 1000

const redirects = tools.map(tool => (
{
    fromPath: `/netlify/${tool}/*`,
    toPath: `/special?tool={tool}&:splat`,
  },
))

// redirects passed to createRedirect function in gatsby-netlify plugin


And i guess the one liner as to why this is a problem.
If you define a redirect rule that sets any queryparams, all original query params are lost (unless you account for them when defining the rule) - this is similar to the old behaviour with redirects where by default paramaters weren’t passed through

Thank you for that explanation, makes a lot more sense now.

Well you could automate this to some extent with the following rule:

/netlify/:tool /special/?tool=:tool&campaign=2022 301!

This would take string from: /netlify/<value>/foo and pass it to the parameter. To add additional parameters, you could add a rule like:

/netlify/:param1/:param2/:param3 /special/?tool=:param1&campaign=:param2&foo=:param3 301!

But you’re right, if you wish to preserve the parameters, you need to mention them in the redirect destination, or else they’ll get overridden.

Yeah it just becomes a lot to maintain if we need another query param added (and its normally other teams who decide to add another param to say a marketing campaign and then engineering doesn’t get informed)

Yeah in that case, I think your only hope is using Netlify Functions. You can then add a simple redirect rule so that all URLs with that specific query param would be routed to a Netlify Function, in which you can process the URL as needed and redirect to the destination.