Redirect by browser language

Hi guys,
I’m trying to setup a redirect based on the language of the browser, tried a lot of variations but can’t find the correct syntax.

Here is my actual entry in the _redirects file:

/ https://wwww.evlaser.it 302 Language=it

What I’m expecting is:
If my language is italian,then redirect me to evlaser.it from every url.

site: https://www.evlaser.com

Thanks a lot!

Hi Federico and sorry to hear about the trouble! First off, I don’t see that redirect on that site - there are about 40 non-language 301 redirects like this:

"/prodotti/ev-smart-2/" to "/prodotti"

so, either you removed it, or you are not deploying what you think you are.

Next up, with that configuration, you’ll redirect ONLY the main page and ONLY if you DON’T have an index.html so I think that is probably not what you want :slight_smile: If you want all URLs to redirect regardless of what content is on the english language site, you want something more like this:

/* https://www.evlaser.it/:splat 302! Language=it

or if you really do want to redirect just the landing page, you still need the ! (cf Rewrites and proxies | Netlify Docs):

/ https://www.evlaser.it/ 302! Language=it

If you give that a try and it doesn’t work, let me know!

That will cause all pages, even ones

Hi fool! thanks for the reply.
I had the rules inside the _redirects file.

Now I’ve placed your examples inside the netlify.toml file but during build it gives me this error:

Invalid netlify.toml file: Error while decoding file /opt/build/repo/netlify.toml: Near line 1 (last key parsed ‘’): bare keys cannot contain ‘/’

I’m a bit confused now in which syntax is correct for the netlify.toml file, I’ve also tried:

[[redirects]]
from = "/"*
to = “https://www.evlaser.it/:splat
status = 302
force = true
conditions = {Language = “it”}

with no luck, I get this error during the build:
Invalid netlify.toml file: Error while decoding file /opt/build/repo/netlify.toml: toml: cannot load TOML value of type string into a Go slice

The examples that @fool mentioned is for the _redirects file. Those examples will need to be converted to the structured format if you want to use them in your netlify.toml file.
This is how the redirect should look like:

[[redirects]]
from = "/*"
to = “https://www.evlaser.it/:splat”
status = 302
force = true
conditions = {Language = [“it”]}

For reference, this is mentioned in our netlify.toml reference doc.

Thanks @Dennis, now I don’t get the errors on build anymore, which is nice.
But still, no redirect is happening.

It works only if I use the country condition, not the language ones.

Hi @federicovezzoli can you please provide a link to the deploy that you are testing and the URL that you are testing with? Also does your browser have multiple languages set? Can you test with sending a single language and let me know if that works? Note that I don’t believe people should limit the number of languages, this is for debugging purposes.

I can confirm, that a simple lang redirect form root is not working:
/ /de/ 301 Language=de
/ /es/ 301 Language=es
/ /en/ 301

This ALWAYS redirects to /en and never matches the languages.
Any idea ?

Hi @derhaeuptling.com, I think you asked about this in another thread already, and I asked you for more info there :wink:

Hi @futuregerald or @Dennis ! I was wondering if it would be possible to do something like:
[[redirects]]
from = “/*”
to = “/fr/maintenance”
status = 302
force = true
conditions = {Language = [“fr”]} /

[[redirects]]
from = “/*”
to = “/nl/maintenance”
status = 302
force = true
conditions = {Language = [“!fr”]} // All languages except fr

But I guess these above would be conflicting? (at least it didn’t work)

Using a bang (!) is not supported. However, since our system parses redirect rules from top to bottom (cascading), the second redirect rule doesn’t have to have the Language conditions since any ‘fr’ requests will never reach that redirect. Essentially, the more specific rules (like that ‘fr’ language redirect) should be on the top of the file. Let me know if that makes sense.

The forum software is asking me

Oh yes, I’m absolutely positive that I want to revive this topic. The language-based redirect works if only one language is present in the Accept-Language header and only if no quality value is given. This feature is useless in its current form and only causes confusion. Even a vanilla installation of Chrome sends a header that cannot be interpreted by your redirect engine: en-US,en;q=0.9. I think you should either complete this feature or admit that you’re not going to implement it and simply remove it from the documentation.

Hey @revl,

Yeah, Country redirects are much more widely… functional? compared to Lang redirects at this present time. However, a lot of work is going in to this area so stay tuned! :slight_smile:

1 Like

That’s OK. Thanks for the update. We’ll wait, impatiently. To be clear, country redirects are a poor substitute for language redirects. Canadians from Quebec can attest to that.

Hi @Dennis Hi @federicovezzoli you manage to successfully redirect based on the country, i’m tryin to achieve it, but it doesn’t seem to work.
I have to domain setup on www.w-architectures.com as primary and www.w-architectures.ch as domain alias.
I want to redirect switzerland users to the .ch domain.
This is my toml
[[redirects]]
from = “/*”
to = “https://www.w-architectures.ch/:splat
status = 302
force = true
conditions = {Country = [“ch”, “CH”]}

Any clue?
Thx

Hey there!

I can’t see that particular rule for the deployed site, only rules for /de and /de/:splat.

Can you confirm what you’re trying to achieve?

Still having the issue
Any help?
Dis someone solve this?

Hey! Maybe you missed my last post:

Hi thx a lot for the reply.
I want people in switzerland to be redirected to the .ch domain. But I ended up having infinite redirects.
That is why you don’t see it.

[[redirects]] from = “/*” to = “https://www.w-architectures.ch/:splat 1” status = 302 force = true conditions = {Country = [“ch”, “CH”]}

Both .ch and .com are set on the same netlify project. The .ch works the same as the .com, but can’t seem to redirect users based if they are from switzerland.

Any help would make my client happy.

Ah, the rule might infinitely loop because /* will also try and redirect the .ch site using the rule!

You’ll need to specify the full path in the from condition:

from = "https://www.w-architectures.com/*"

thx for the reply.
I’ll try that.