Allow or block visitors from specific countries using .htaccess
Once launched, a website is available worldwide. However, in many cases, it is necessary to control access to your site, especially when you want to stop spammers and hackers from certain countries.
Step 1: Generate the country IP addresses
There are many online tools available to allow or block visitors based on their country. In this tutorial, we will use country IP blocks .
- go to the home page of Country IP Blocks.
- Select the countries you want to block or allow.
- In the section select format , choose Apache .htaccess Deny either Apache .htaccess Allow. The choice depends on whether you want to block or allow visitors from those countries.
- Click on Create ACL .
- The ACL results They will appear on the right side of your screen. Press the button Copy ACL to clipboard.
- If you are using the function to allow , be sure to insert the line deny from all which is above the list of allowed IP addresses.
Step 2 – Insert the generated IP addresses into the .htaccess file
Finally, paste the IP addresses of the countries you want to block or allow into the file .htaccess