This article provides you with information on how to Block IP address and specific countries from your website by editing your .htaccess file.
What is .htaccess ?
.htaccess is a configuration file for use on web servers running the Apache Web Server software. All our Linux hosting control panels have Apache running on the back end. These .htaccess files can be used to alter the configuration of you hosting space to enable/disable additional functionality and features. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions.
Configuring your .htaccess file
This file is located in the public_html folder of your primary domain, and the primary folder of any subdomains and add on domains. Just add the following code to the top of your file:
*** All IP address below are examples, you will need to replace them with the IP addresses you intend to block from your site.
deny deny from 18.104.22.168
deny from 22.214.171.124
allow from all
What if the unwanted visitor is from a different country and keeps changing their IP? Provided your website isn’t intended for visitors from that country, you can easily use the GeoIP tool to block that country. Just add the below code to the top of your .htaccess file.
# Add countries you wish to deny here
SetEnvIf GEOIP_COUNTRY_CODE CO DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE EG DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE HI DenyCountry
Allow from all
Deny from env=DenyCountry
INFO: A list of all country codes can be found here.
TIP: Should you require more information regarding .htaccess we would advise this article here.