I have daily received robot traffic to my website with the intention that I lose my Google AdSense account . I have been able to handle the situation very well and I have blocked all the countries that the robot traffic comes from using this code in my .htaccess file
# Lista de países bloqueados
GeoIPEnable On
SetEnvIf GEOIP_COUNTRY_CODE RS DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE US DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE MX DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE AU DenyCountry
SetEnvIf GEOIP_COUNTRY_CODE NL DenyCountry
Allow from all
Deny from env=DenyCountry
What really strikes me is whether the cities of the countries can be blocked using this same code, only by changing the iso code .
Other solutions
Currently I can block a city using PHP code, getting the location of the visitor and creating a condition to remove it from the page or send it to a specific place, and that would be fine too, but I think it would be much more interesting if I could control with htaccess
Please if anyone knows anything about this topic, let me know.
I recommend you to use Cloudflare . What this tool does is put itself between your visitors and your server, and with this you save your server from having to do the "dirty" work.
For it to work, you have to point your domain's DNS servers to Cloudflare (they explain how to do this).
It is a very complete tool, and with the free version you can do many things: you can use it to cache your static content and make your page load faster, create redirect rules, protect yourself from malicious bots, use a free certificate so that your page is secure (https),... and a host of other things.
For what you are looking for, they have the Workers option . They have an article that talks about the geolocation of petitions . I just did a quick test and it's relatively easy to implement.
This should do what you're looking for.