How to Block IP Addresses with .htaccess?

This tutorial will explain how to deny an IP address or how to allow only a particular IP address to access your website. This is extremely useful to protect your website from hackers or to block unwanted visitors.

You need to create an .htaccess file in your public_html folder and add these lines below, based on your requirements.

To Deny a Specific IP Address

The below code will only deny one IP address from accessing the website.

​​Deny from 192.0.0.1

Just change the IP address to the IP address that you want to deny and paste it in the .htaccess file.

To Deny a Specific Domain

We can deny a specific domain from accessing our domains by adding the below line of code to your .htaccess file.

Deny from 192.0.0.1/255.255.255.0

You need to change the IP address and netmask value to match the domain that you want to block.

To Deny Multiple IP’s in a Single Line of Code

Leave a blank space with each IP for separation.

​Deny from 111.111.111.111 222.222.222.222 333.333.333.333

All you need to do is replace the IPs that you want to deny and paste it in the .htaccess file.

To Allow Only a Single IP

The below set of .htaccess rule will deny access to the website from every IP address expect the IP address mentioned in the rule.

Order Deny,Allow
Deny from all
Allow from 111.111.111.111

Here we first deny all visitors, and then allow only the visitor from the IP address specified above.

To Deny a Single IPv6 Address

To block a IPV6 address via .htaccess, follow the syntax below:

Deny from 2001:cdba:0000:0000:0000:0000:3257:9652

Conclusion

Now, when a user tries to visit your site from a blocked IP address, they would see a 403 Forbidden page instead.

  • block ip address using htaccess
  • 0 Users Found This Useful
這篇文章有幫助嗎?

相關文章

How to show hidden files (.htaccess) in cPanel File Manager?

Many users ask us how they can see the .htaccess files for their websites in cPanel File Manager....

How Can I Compress / Uncompress Files Using the File Manager?

How to Compress Files Go to the File Manager. Navigate to the folder where the files you want...

How to View Your cPanel Disk Space Usage?

In order to view your cPanel disk space usage, there’s a great tool located inside of your...

Accessing webmail for your domain

To access the included webmail email clients included with your hosting account, you’ll need to...

How to rename a database in phpMyAdmin?

phpMyAdmin is a free and opensource application for MySQL database management. It is one of the...