How can I block specific countries accessing Magento 2 site?
You can block access to specific countries either in .htaccess or free Magento extensions available
Find the reliable list of IP's for the country you want to block from IP2location
1: https://www.ip2location.com/free/visitor-blocker. Input the country name and generate the IP's list for the country you want to block. Open the .htaccess file and copy the code from downloaded file similar to the lines below
order deny,allow deny from 126.96.36.199/23 deny from 188.8.131.52/32 deny from 184.108.40.206/32 allow from all
Save the changes and the IP's from these countries will be denied access to Magento store.
- Else use this free extension to configure the countries to be blocked in the backend so the module does other work to restrict access for specific countries https://www.mageworx.com/geo-lock-magento2-extension.html
Every country is assigned a range of IP addresses. You can allow or deny your website from being accessed from a specific IP or a list of IP addresses by using the .htaccess method.
- Prepare the list of such ranges which you want to block by the help of https://www.countryipblocks.net/country_selection.php or https://www.ip2location.com/free/visitor-blocker or any other similar website.
- Put the list in the .htaccess file of your website.
- Restart the server to take the .htaccess changes effect.