Support Forums

Preserve query parameters on redirect

Hi everyone, I’ve implemented the following in our netlify.toml file:

  from = "/"
  to = "/fr?utm_source=:source&utm_medium=:medium&utm_campaign=:campaign"
  force = true
  query = { utm_source = "source", utm_medium = "medium", utm_campaign = "campaign" }

From my tests, any combination of these 3 query parameters (including 2, 1 and no query parameters) in the URL lead to a correct URL. Am I missing something, or is this indeed enough to redirect for all these 3 parameters?

Hey @Floriferous, can you elaborate on what a ‘correct URL’ is? What is the true intended URL?

There’s a sample file here and this discussion will be useful for you, too!

Correct means:

  • /?utm_source=A/fr?utm_source=A
  • /?utm_medium=B&utm_source=A/fr?utm_medium=B&utm_source=A
  • Etc. no matter the order and amount of the 3 query strings I pass.

Gotcha. Unfortunately, you’re going to need a redirect rule for each of the possibilities.

If your rule specifies three parameters, our engine will expect to see all three parameters but it doesn’t matter what order they’re in. For example, this rule:

  from = "/origin"
  to = "/new/origin"
  status = 301
  query = {one = ":one", two = ":two", three = ":three"}

Will successfully redirect the following:

  • /origin?one=yes&three=yes&two=yes
  • /origin?one=yes&two=yes&three=yes
  • /origin?one=yes&two=&three=
  • /origin?three=yes&two=&one=

It will not redirect /origin?one=yes.

Repro: https://redirect-multiple-query-strings.netlify.rodeo

I tried your reproduction, and it doesn’t seem to work for /origin → /new/origin

From what I can tell, it works on our production website without specifying all other possibilities?

I would expect that you need a rule without the query string parameters, to redirect /origin to /new/origin.

If you’re not seeing this and this is still an issue, do you mind sharing your site’s app ID and/or applicable deploy ID so I can see what rules you have and check what’s actually happening?

1 Like

Sure, it’s www.e-potek.ch, and the redirect is from / to /fr.

Hey @Floriferous,

I see what’s happening – it’s because we automatically pass through query string parameters now! The query strings would always be passed through, it’s just what we do.

So, you won’t actually need the second-to-last rule with the query string rules, if you want to pass these parameters through.

If you don’t want to pass them through, you’ll want to instate something like this (see section Query String Parameters).

Your last reply is a bit confusing, do we need to specify query = { } in the netlify.toml or not now?

What “second-to-last” rule are you referring to?

Have we been talking all along about outdated product information?

Hey @Floriferous,

It depends what you’re trying to do and I’m struggling to understand what you want.

If you want any sort of query string to be included in the redirect, you don’t need to specify it in a redirect rule. If you want all query strings to be passed through, remove the redirect rule containing query = { } (this was the second-to-last rule I was referring to)

If you want only specific query string parameters to be included in the redirect, you’ll want to follow the advice from before and observe the reproduction I created.