I see a few people with _redirects issues but didn’t find a post that matched my exact situation and didn’t seem fair to hijack related posts.
Based on this reference in the docs—https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language — it worked for me only on Safari but failed in Chrome and Firefox (running MacOS Catalina).
I want to redirect based on visitor’s country first then update the path based on the user’s language setting. The instructions, albeit using Israel and Hebrew in its context, is essentially the blueprint I wanted to succeed. My site, too, structures its URL following domain/country/language/*
. My specific testing context is this: I have visitors coming from some African countries and I want them redirected to /southafrica
; I also have Canadian and US visitors that I’d like to redirect to /northamerica
.
Specifically, I have a _redirects file:
/ /northamerica/ 302 Country=ca,us
/northamerica/* /northamerica/en/:splat 302! Language=en
/ /southafrica 302 Country=ao,bw,cm,cg,cd,ke,mg,mw,mu,mz,na,za,tz,ug,zm,zw
/southafrica/* /southafrica/en/:splat 302! Language=en
# fallback for other countries?
/ /northamerica/en 301
Although my rules show only English at the moment, I wanted to ensure the way the rules are set up perform correctly so I may add additional language redirects.
Additional background:
- site I am testing is https://dev--almex-group.netlify.app
- I have a netlify.toml file but it does not contain any redirect rules
- I am building my site off a Gatsby foundation
- the _redirects file is stored in the
/static
folder in the project as per https://www.gatsbyjs.org/packages/gatsby-plugin-netlify/ - the rules appear to be deployed correctly, build log shows
5 redirect rules processed. All redirect rules deployed without errors.
- rules were tested successfully in https://play.netlify.com/redirects although I may have specificity in the wrong order
- Chrome and Firefox only managed to redirect appropriate Africa-originating users to
/southafrica
which triggered my 404 error rather than have the second redirect. I thought I might skirt this behaviour by employed the force=true flag (!
appended to302
) but to no avail:
- North American users would appear to get redirected to
/northamerica/en
but apparently only because of the fallback rule; when I removed that rule, users did not get redirected at all when hitting/
. - Safari correctly redirected users to
/southafrica/en
and/northamerica/en
based on the user’s origin. Inspector showed 2 redirections were detected based on the screenshot below
Hoping someone can shed some light…
—Victor