Preserve query parameters on redirect

I’m trying to preserve query parameters when redirecting internally, but haven’t found the right combination of rules in netlify.toml.

An example of what I’m trying to achieve would be having:
/author/brittany-yoon/?utm_source=facebook
retain the query parameter when redirected to /blog/?utm_source=facebook.

I’ve tried using :utm_source in the query, from and to values for a [[redirects]] rule in netlify.toml, but no luck yet.

Can someone help point me in the right direction? From the documentation I’ve read, it’s possible to redirect and convert query parameters to part of a path string but I’m trying to retain my query parameters on a new path.

Thank you!

Hi @vanwap,

You should be able to include your query params in your redirect rule. There is an example in our netlify.toml reference doc.

Thank you @futuregerald.

The documentation seems to reference doing this for converting a query string to a path, but I did figure out how to get it working with just the query string to query string instead.

It’s just unfortunate that every single possible combination of query string parameters has to be defined.

hi vanwap, would you mind posting your working solution so others can see how you did this?

as far as different redirect behaviour goes, please outline your use case here:

thanks.

1 Like

Hi, I need to preserve some query parameters (all by default would be great). I am trying to do so through my _redirects file but I haven’t been able to do so yet. I have tried things like:

/admin/* queryParam=:qp https://outside.site/:splat?queryParam=:qp 301
/admin/* https://outside.site/:splat 301

But the query param gets ignored anyway, with or without the first rule, and writing the query param on the to url or not. I’ve also tried to force the first redirect, without change. Is there a way to do this with Netlify?

@adrm - does Gerald’s response to this question provide an answer for you at all?

@adrm @futuregerald @perry

Here is the solution I found, although we ended up giving up on preserving our query parameters all together, since using this solution would require thousands of lines of code for only ~100 redirects. The reason for this is we had 9+ possible query parameters for each redirect, meaning that each possible combination had to be accounted for.

Here is a _redirects file example.

/path1/ param1=:value1 param2=:value2 param3=:value3 /path2/?param1=:value1&param2=:value2&param3=:value3
/path1/ param1=:value1 param2=:value2 /path2/?param1=:value1&param2=:value2
/path1/ param1=:value1 param3=:value3 /path2/?param1=:value1&param3=:value3
/path1/ param2=:value2 param3=:value3 /path2/?param2=:value2&param3=:value3
/path1/ param1=:value1 /path2/?param1=:value1
/path1/ param2=:value2 /path2/?param2=:value2
/path1/ param3=:value3 /path2/?param3=:value3
/path1/ /path2/

Done from memory but should be correct. You can see why listing all the possible combinations is a bit laborious, even for a single redirect.

1 Like

You could probably write a script to dynamically generate the _redirects file and output all possible query parameter combinations, to save doing it manually.

1 Like

This is really painful Netlify. We run a geo-localised site that always redirects users to a country specific subdirectory. So when we post content on social media or ads we use our tld/path and then use :splat to make sure customers get the country specific version.

Based on the current implementation our marketing team would have to specify every combination of utm campaign, source, medium etc.

There absolutely needs to be a solution to configure :splat to pass through url parameters.

Yup, that’s effectively the advice we give folks. If you have only a few possible query string args, it’s doable. When you have dozens of paths AND dozens of optional QSA’s then it becomes unwieldy though.

Yup, our Support team agrees and is fighting for the users in the prioritizing of work with our product team. As I think I mentioned to you in the helpdesk, it’s an open feature request to which we’ve added your voice (so we’ll be following up both here and to your helpdesk case, if we implement it).

1 Like

Thanks @fool,
That’s all we can ask for I guess - I appreciate the transparency. We really like the product and some of the new stuff you’re building are ace (dev, large assets, analytics etc). However, if marketing groups are a key target for you, then this is really table-stakes stuff that has to work.

+1 – please implement a “preserve all query params” option!

1 Like

Thank you for prioritizing this issue, @fool! This is a major problem for my company.