Using the .htaccess File to Block IP Access

Using a .htaccess file will grant you greater control over your website, such as safe guarding it from hacking attempts or even keeping out spammers who may frequent your website. A great feature of using a .htaccess file is the ability to populate it with single IP addresses or entire IP ranges, effectively blocking those IP's from being able to access your server. This article will explain how to implement this via the .htaccess file.

You will need to log into your Linux server directly using SSH. 

To create the .htaccess file, you need to create a text file with any Linux editor. The file must be called .htaccess and it must exist in the root directory of the website you want to deny access to.

In the file, there is a specify format you must adhere to. To block both single IP's and IP ranges, you must include the following;

order allow,deny    - The rule set
deny from 192.168.1  - IP you want to block
deny from 24.0.0.0/23   - IP Range you want to block

Please note to block an IP Range, you must know the subnet.

You can also specify a deny all and allow rule set as well;

order deny,allow      - The rule set
deny from all         - Deny access from all IP's.
Allow from 192.12.4.1 - IP you want to allow.

An example of a file blocking IP address 1.2.3.4 and a subnet 2.0.x.x is below:

<Limit GET POST>
 order allow,deny
 allow from all
 deny from 1.2.3.4
 deny from 2.0.
</Limit>
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Installing htop

htop is an interactive process viewer for Linux, which is a powerful alternative to the...

Bind Multiple IP Addresses to a Single Network Interface Card (NIC)

This tutorial demonstrates how to bind multiple IP addresses to a single NICI'll be using LAN...

Clear Memory Cache on Linux Server

By default the Linux OS has a very efficient memory management process that should be freeing any...

Set the time on a Linux server

Log into the server via SSHAt the shell prompt run the following command:date MMDDHHmmYYYYFor...

Check your disk usage in Linux

A good way to check your disk usage for folders on your Linux server is via shell.Download...