How to block a whole country from visiting your website

There are times when you may want to ban visitors from a certain country from viewing your website, the reasons for these can go from geolocation restricted content, to stopping malicious hackers.

You can block people from viewing your website using rules in your firewall IP tables but if you don’t have access to that, you can edit the .htaccess file and add a list of IPs to be blocked, you can get the whole IP range from a country from IPInfoDB.

However this solution may cause server overload if many requests are made from that country, resulting in a denial of service side effect, an easier way to do this, is by banning people from accessing your website according to their browser language settings, for example to ban people who is using a Chinese internet browser you will add to your .htaccess file:


RewriteRule .* – [F]

This will generate a 403 error message with Chinese browser configured visitors getting a “Forbidden” message when visiting your page, the language settings can be changed to any language you like,  zh-CN is the browser language settings for Chinese.

Leave a Reply

Your email address will not be published. Required fields are marked *