apache tutorial - How to control site access based on a visitor's country - apache - apache web server - apache server - apache2



  • To allow or block visitors based on the country associated with their IP address. You may want to do this for a variety of reasons, including:
    • Repeated attacks from IP addresses originating in a specific country or countries.
    • Visitor statistics that are misleading or difficult to intrepret due to numerous visits from a specific country or countries.
    • For example, site statistics may indicate a very high bounce rate due to visits by botnets rather than actual humans.
    • If you run an e-commerce site, repeated fraudulent orders.

Controlling site access by country

  • You can control site access from specific countries by using allow and deny directives in an .htaccess file.
  • To do this, you must obtain the ranges of IP addresses that are assigned to a particular country.
  • There are several web sites that enable you to generate these .htaccess directives automatically based on the country or countries you specify.
  • The following procedure demonstrates how to use the service provided by IP2Location.
  • This method is not 100% effective because IP address assignments can change, and IP address ranges can overlap.
  • Nevertheless, this method blocks the majority of traffic from the specified country.
  • Some countries are allocated numerous IP address ranges, and blocking all of them requires hundreds or thousands of lines of .htaccess directives.
  • Because the server reads and processes the .htaccess file each time your site is accessed, this may cause performance issues.
  • You should test any IP address blocking configuration to make sure it does not negatively impact your site's performance.
    • Use your web browser to visit https://www.ip2location.com/blockvisitorsbycountry.aspx.
    • Under Step 1, confirm that IPv4 is selected.
    • Under Step 2, select the country or countries for which you want to block or grant access.
  • To select multiple countries, press the Ctrl key while you click.
    • Under Step 3, in the list box choose one of the following:
    • If you are granting access to the country or countries you selected in step 3, select Apache .htaccess allow.
    • If you are blocking access to the country or countries you selected in step 3, select Apache .htaccess deny.
    • Click Download, and then save the file on your local computer.
    • Open the downloaded file in a text editor, and then copy the entire block of generated text.
  • Depending on the country or countries you select, the block of text may be quite long.
    • On your A2 Hosting account, open the .htaccess file in your preferred text editor.
    • Paste the block of text that you copied in step 6 into the .htaccess file.
    • Save your changes to the .htaccess file. The new configuration takes effect immediately.

Related Searches to How to control site access based on a visitor's country