Disclaimer: I’d like to make country-based redirect for legal purpose only.
I created this site: https://app.dev.planify.cloud/ (name: vigorous-kepler-e8e853) and I’d like to redirect users from other countries than France toward my /unavailable URL.
I didn’t see how to make a reverse condition (not in FR) so, at least, I wanted to redirect US and CA countries.
I created this _redirects file:
# Redirect users in USA or Canada to /unavailable
/* /unavailable 302! Country=US,CA
This link (Redirect options | Netlify Docs) didn’t give me all details to achieve the redirect (how to test it ? I was using VPN but maybe other ways are more suitable).
could you help me with the reverse redirect (not in FR) ?
could you tell me how to test the redirection without moving from my desk ?
In this example, anyone from USA or Canada will go to the sorry page, anyone from France the welcome page, and anyone from any other location will get the normal index page.
There is no way to configure redirects to REDIRECT anyone NOT in [COUNTRY] (that I am aware of) so what you would likely do with your _redirects is
/ /welcome 302! Country=fr
/ /index
Which will direct anyone in France to the welcome page while anyone outside France will see the standard homepage.
Note: I tested this using a VPN, which I course means anyone can get around your redirects using the same method. Isn’t a lot you can do about that .
You can send a cookie with your request. In browsers, you can simply add a cookie with the name nf_country and value as the 2 character code. In curl, you could specify the --cookie option.
About, not in FR, yes, @coelmay is correct, there’s no way (yet) to negate a redirect rule.
a user from CA or US comes to my site, I want Netlify to redirect him to the appropriate page, due to the country redirect feature (without the user to browse my homepage in order to create a cookie and then it can be redirected, though, it can be helpful if the first solution cannot be achieved).
I don’t know if this will make a difference, but examples in Netlify Docs use lowercase country codes when in the _redirects file while using uppercase in the netlify.toml. Perhaps changing to lowercase might work?
Thank you for the HAR file. From the Request ID, I was able to check the IP address of the requester and it turns out to be from US. So at least the location is being detected correctly.
Now as to why the redirect is still not working, the strange thing is, I’m not seeing any redirects in the deploy. This is the deploy I’m talking about: Netlify App. The log even says no redirect rules processed.
Would you please mention how you’re adding the redirects?
It seems that I didn’t see this: Save a plain text file called _redirects without a file extension to the publish directory of your site. You can find _redirects file syntax details below.
I created a _redirects file but I saved it in root directory.
I’ll try another deploy with the _redirects file in the publish directory and test it again.
I used the netlify.toml file to create my redirect rules because of the simplicity in my context (deploy a SPA generated Nuxtjs App): the netlify.toml is placed in the root directory.
I just tried to navigate through my VPN (in USA), I was redirected to the /unavailable page and I was not redirected when I disactivated the VPN, browsing from France
Would be great if the ‘Not in [country]’ rule could be possible though.