For Linux Hosting

Learn how to use an .htaccess file to deny certain IP addresses from accessing your website.

Blocking an IP address may be necessary if you are aware of that IP addresses attempting to abuse or attack your website.

Step 1

Create a new text file and name it .htaccess.

Please note: Use a basic text editor such as "Notepad" in Windows, "TextEdit" for Mac OS X, or "vim" for Linux. If you already have an .htaccess file on your web space, you can download it first and then edit it.

Step 2

Enter the following code in the .htaccess file, replacing 1.2.3.4, with the IP address you want to block.

<Limit GET POST>
order allow,deny
allow from all
deny from 1.2.3.4
</Limit>

Explanation:

  • Line 1: Specify which HTTP methods are to be restricted.
  • Line 2: Rules that allow connections should be processed before rules for denying connections
  • Line 3: All connections should be allowed (unless specified otherwise)
  • Line 4: Exceptions to the rule in the line above
  • Line 5: End of the section

You can also prohibit access to multiple IP addresses or entire IP address ranges, as shown below:

<Limit GET POST>
order allow,deny
allow from all
#Multiple single IPs
deny from 1.2.3.4
deny from 1.2.3.5
deny from 4.3.2.1
#Entire IP range
deny from 2.3.4.0/21
</Limit>
Step 3

Upload the .htaccess file to your web space. The parameters set in the .htaccess will apply to all subdirectories.

To prohibit connections to the entire web space, save the file in the main folder. If you only want to prohibit access for a specific web page, save the file in the subfolder that contains the web page.

The settings will take effect immediately once uploaded.