Keep incoming query string params and append a new param during redirect (instead of replacing)

In my netlify.toml I have a redirect like this:

[[redirects]]
  from = "/"
  to = "https://eu.example.com/?lang=de"
  force = true
  conditions = { Country = ["DE"] }

Since I am providing my own query string param, the params that were present in the initial request are lost. I understand from this thread that this is by design.

Is there a way to keep BOTH the original query string params and the new ones defined in the redirect rule itself?

Hi @mzrnsh,

Welcome to the Netlify community.

So when you say original query string you are meaning someone visits your site at

https://eu.example.com/?query=string

and you would like to keep this so that based on the condition in your example you would have

https://eu.example.com/?query=string&lang=de

or

https://eu.example.com/?lang=de&query=string

Is this correct?

Hi @coelmay. Let me provide an example. Let’s say my netlify site is example.netlify.app.

When someone in Germany visits this URL:

https://example.netlify.app/?click_source=google_ad

I want them redirected to:

https://eu.example.com/?lang=de&click_source=google_ad

where both the original query string params (click_source) and the new one set from the redirect (lang) are present.

But what happens actually is that the original query string is not preserved and they are redirected to this URL:

https://eu.example.com/?lang=de

meaning the original query string is replaced and not joined with the new one. This seems like an intentional decision but I am wondering if there’s a workaround.

Yeah, that’s totally possible! I’ll cover a few different examples and explain how they work, finishing with what you’re looking for :slight_smile:

To start, the following example shows how we automatically pass through a query string, when no query string is present in the to path.

/store lang=:lang  /blog

/store?lang=DE will result in a redirect to /blog?lang=DE.

In the next example, because we explicitly define a query string parameter in the to path, the query string in the from path is dropped.

/store lang=:lang  /blog?click_source=google_ad

/store?lang=DE will redirect to /blog?click_source=google_ad.

And lastly – we want the best of both worlds. So, let’s explicitly define the two parameters!

/store lang=:lang  /blog?lang=:lang&click_source=google_ad

/store?lang=DE will redirect to /blog?lang=DE&click_source=google_ad.

I hope this helps!

2 Likes

Thanks for the answer.

I was probably not entirely clear with my question. Let me clarify a few points:

  • ?lang=DE is not a part of the incoming query string. Rather, I want Netlify to recognize visitors from Germany and add this query string to the destination URL. And to be fair, Netlify already does this pretty well.
  • The query string example I provided (?click_source=google_ad) is really just an example. It could have a different value, for example ?click_source=blog_article. Furthermore, there could be other query string parameters which we do not know in advance. So I am after passing through ALL query string parameters, just like Netlify does with recent changes for to URLs that contain no query string.

Let’s have a look at my config file:

# netlify.toml

[[redirects]]
  from = "/"
  to = "https://eu.example.com/?lang=de"
  force = true
  conditions = { Country = ["DE"] }

[[redirects]]
  from = "/"
  to = "https://eu.example.com/?lang=fr"
  force = true
  conditions = { Country = ["FR"] }

In the above example, my website is example.com, hosted on Netlify. If someone visits this URL from Canada, United States, or just about any country except Germany or France, all good, example.com should open just fine.

However, if a visitor comes from Germany or France, they should be forwarded to https://eu.example.com. In addition, eu.example.com supports multiple languages, and language is set via lang parameter. So, I am using Netlify redirects to provide that query string.

With current config, German visitors are sent to https://eu.example.com/?lang=de and French visitors are sent to https://eu.example.com/?lang=fr

It works well except cases when the original URL contained some query string parameters (that I don’t want to lose). Let’s go through examples:

No incoming query string

  • A visitor from Germany opens
    https://example.com
  • They are forwarded to
    https://eu.example.com/?lang=de

ALL GOOD

Incoming query string is present

  • A visitor from France opens
    https://example.com/?foo=bar
  • They are forwarded to
    https://eu.example.com/?lang=fr

NOT GOOD, because ?foo=bar is lost and never made to the destination website.

So to go back to my question: is it possible to change my config in a way that in the 2nd example the visitor would have been forwarded to https://eu.example.com/?lang=fr&foo=bar instead.


Hope this clarifies things. I understand very well that Netlify is not designed to be a URL forwarding solution (and it’s pretty amazing at things it is designed for!). Yet I’d say it’s also quite close to being a perfect URL forwarding tool and that’s why I am so demanding :smiley:

To be able to use a query string and add it to a redirect, Netlify needs the query string to be explicitly defined in the config file. So, if you’re looking to support any query string, that won’t be possible for now. You might have to look for Netlify Functions or handle this in client-side JS.

Chances are in future if/when Edge Handlers become public, you’d be able to do this, but not at the moment. However, we can file a feature request.

1 Like