Knowledgebase Home / How to Block IP Addresses with .htaccess

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.

We'd love to hear your thoughts. How can we make this better?
Yes
No
Powered by Helpful

If the steps above listed in this article do not resolve your issue, please feel free to open a support ticket and we’d be happy to take a look.

Looking for better web hosting? Sign up today and take 50% off your first month with promo code: 1stmonth50

Our Guiding Principles

  • Provide consistent, stable and reliable web hosting services.
  • Ensure rapid ticket response and quick resolutions to issues.
  • Never saturate or over-provision servers to ensure stability and speed for our customers.
  • Use only high quality enterprise-class hardware to ensure minimal downtime from hardware failures.
  • Provide clear pricing with no hidden fees or gotchas.