[[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?
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.
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
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.