How to Block IP Addresses Using the .htaccess File

The .htaccess (hypertext access) file is a powerful configuration file used on web servers to control various aspects of website behavior. Among its many functions, one of the most crucial is the ability to block specific IP addresses or ranges from accessing your website. In this comprehensive guide, we’ll walk you through the steps to block IP addresses using the .htaccess file, helping you enhance your website’s security and manage access effectively.


Before you begin, make sure you have the following prerequisites in place:

  1. Access to Your Web Server: You need access to your web server’s file system to create or modify the .htaccess file.
  2. Text Editor: Use a text editor such as Notepad (Windows), TextEdit (macOS), or a code editor like Visual Studio Code for editing the .htaccess file.

Step 1: Locate the .htaccess File

The .htaccess file should be in the root directory of your website. If it doesn’t exist, you can create one using a text editor. Ensure that the file is named exactly “.htaccess” (including the dot at the beginning) with no file extension.

Step 2: Create Rules to Block IP Addresses

To block specific IP addresses or ranges, add the following lines to your .htaccess file:

# Block a single IP address
Deny from 123.456.789.10

# Block a range of IP addresses
Deny from
  • Replace 123.456.789.10 with the IP address you want to block.
  • Replace with the IP range you want to block. In this example, it blocks all IP addresses from to

Step 3: Save and Upload the .htaccess File

After adding the IP blocking rules, save the .htaccess file. If you created the file from scratch, ensure you save it with the correct name (“.htaccess”) and no file extension.

Upload the .htaccess file to the root directory of your website using FTP or your web hosting control panel.

Step 4: Test the IP Blocking

To test whether the IP blocking is effective, attempt to access your website from the blocked IP address. You should receive a “403 Forbidden” error, indicating that access is denied.

Step 5: Additional Considerations

  • Blocking Multiple IP Addresses: You can add multiple “Deny from” lines to block multiple IP addresses or ranges.
  • Allowing Specific IP Addresses: To allow specific IP addresses while blocking all others, you can use the “Allow from” directive. For example:
    # Allow a specific IP address
    Allow from 123.456.789.11
  • Custom Error Pages: Customize the error message users see when blocked by adding an ErrorDocument directive to your .htaccess file. For instance:
    ErrorDocument 403 "Access Denied - Your IP address is blocked."


Blocking IP addresses using the .htaccess file is a powerful method to enhance your website’s security and control access. However, it’s essential to use this capability judiciously and maintain an up-to-date list of blocked IP addresses. Regularly review your .htaccess file to ensure it serves its intended purpose and doesn’t inadvertently deny access to legitimate users. By mastering this technique, you can better protect your website from malicious or unwanted visitors.


Related articles :

Leave a Reply

Your email address will not be published.