Blocking certain countries/regions with __redirects while using react app

Hello, I have a problem.
I would like to block few countries before they hit the website.
Will start from saying that I am using react application, that means I have to redirect all the subpages to index.html, in order for react router to show correct page.

The problem is that it is using (I mean not really a problem, it does what it should)

__redirects file
/* /index.html 200

but now when I want to block few countries
I have added this block of code above

/*  /blocked.html  301  Country=ru,cn

/  /blocked.html  301  Country=ru,cn

This works and actually blocks those countries, but only if someone opens the domain like that

domain.com/sub-page

If you open from root

domain.com

it will still hit the domain without blocking the country. (and that is bad)

This is my full code for the __redirects file

What can I do in order to make it redirect root also for those countries?

/*  /blocked.html  301  Country=ru,cn

/  /blocked.html  301  Country=ru,cn

/* /index.html 200

That’s not a Netlify issue - it’s a React problem. React uses JavaScript to render content, which works without requesting additional data fro the server once the initial page has loaded. There’s no way to stop it from server-side controls and even if you do, someone can save your website’s JS bundle along with HTML file and see the entire website locally.

You should look into a framework that can provide a static HTML output (and thus, won’t rely 100% on JS to switch pages), or use a server-side rendered framework.

But if someone hasn’t hit yet the website, shouldn’t the netlify engine redirect them if the country is the blocked one, even before they load the content?

Oh wait, now that I re-read your original post, I think I was off-the mark. Sorry about that.

Could you try using 301! instead of 301 to see if it fixes it?

That indeed has solved the problem, thank you :pray:

One more question. Is there somewhere list of country codes (does it follows some specific naming or just the ISO codes?) Like here

This is documented here:

Specifically, this line:

The Country attribute accepts ISO 3166-1 alpha-2 country codes.

1 Like