Home
Support Forums

How to make a country redirect

Hi :wave:

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).

  1. could you help me with the reverse redirect (not in FR) ?
  2. could you tell me how to test the redirection without moving from my desk :wink: ?

Thanks :pray:

Welcome to the forums @tirabc

This is what I have as a test for a _redirects file

/   /sorry   302!   Country=us,ca
/   /welcome   302!   Country=fr
/*   /index

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 .

2 Likes

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.

2 Likes

Hey,

Thanks for your reply.

I tried with this _redirects:

# Redirect users in USA or Canada to /unavailable
/auth/signup/  /unavailable 302! Country=US,CA
/  /unavailable 302! Country=US,CA

My issue is that I am not redirected when using VPN in USA (Proton VPN).
I still can access Planify, gérez vos RDV et votre temps or https://app.dev.planify.cloud/ :frowning:

I tried to add trailing slash in my app but it didn’t work (based on this Nuxt, Netlify and the trailing slash - DEV Community).

Any tip to fix this ?

Thanks for your reply,

My usecase here is:

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).

Hi @tirabc,

Could you send a HAR file recording? It can help us check what might be happening.

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?

Just putting it out there.

1 Like

Yes, I tried both, didn’t work in both cases but I will try again

Of course, I activated my VPN in USA and recorded this HAR:

Hi @tirabc,

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?

2 Likes

Thanks for checking my issue.

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.

:man_facepalming:

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.

Ok, it works :white_check_mark:

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 :raised_hands: and I was not redirected when I disactivated the VPN, browsing from France :+1:

Would be great if the ‘Not in [country]’ rule could be possible though.

Thank you for your help @hrishikesh and @coelmay :pray:
Have a great day !

2 Likes