Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  cPanel  >  Steps to block IP address with .htaccess
Top Scroll

Steps to block IP address with .htaccess

 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 –

Deny from 192.0.5.1

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 –

Deny from 192.0.0.1/366.366.366.0

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.

Deny from 111.111.111.111 444.444.444.444 666.666.666.666

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 :

Order Deny,Allow
Deny from all
Allow from 222.222.222.222

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 :

Deny from 2001:cdba:0000:0000:0000:0000:3284:7536

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?

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.