Knowledge Base
MilesWeb / cPanel

Steps to block IP address with .htaccess

Approx. read time : 2 min

In this tutorial, we will learn how to deny an IP address or how to allow a particular IP address from accessing your website. This measure is taken to save the website from hackers or to block unwanted visitors.

First of all, you will need to create a .htacsess file in your public_html folder and add the following lines as per the requirements:

1) Code to deny a specific IP address –

This code will deny one IP address from accessing your website. Make sure you use the IP address for which you wish to work.

2) Code to deny specific domain –

Using this code you can deny a specific domain from accessing your domain. Use the code in .htaccess file, and replace IP address and netmask value to match the domain that you wish to block.

3) Code to deny multiple IP’s in a single line of code –

While using this code, leave a blank space with each IP for separation.

Replace IPs of your choice in .htaccess file.

4) Code to allow only a single IP –

The below code will deny access of all the other websites from every IP address except the IP address mentioned in the rule :

This code indicates that first, we have denied all visitors and then allowed only the visitor from a specified IP address.

5) Code to deny single IPv6 address –

Use the following code to block IPv6 address through .htaccess file :

That’s it!

Also Read :

1) Learn to Block A User Agent in An .htaccess File

2) What Are The Common Reasons For A Website To Be Inaccessible?

With an interest in doing something creative daily, Sonam works as a Digital Marketing Executive. She likes to write technical blogs related to web hosting, digital marketing, and other IT topics. She also likes to spend her leisure time on social media to find different strategies for client engagement.
Need help? We’re always here for you.