How to block IP address using .htaccess


Reviewed by:
On January 24, 2015
Last modified:January 21, 2016


There are times when you need to block a user from accessing your website. Learn how to block IP address using .htaccess file.

There are times when you get many requests form a specific IP, this can be an alarm for you.You might be facing a security threat, or a spammer trying to deliver some spam comments on your blog.

block-visitor-from-IP-address-using-htaccessThe simple solution to it is to block IP address using .htaccess file. It will restrict the user of that IP from accessing your website.

All you have to do is put a specific code in your .htaccess file, then the .htaccess file will deny all the requests generated from that specific IP address.

How to deny access to an IP address using .htaccess method

First you have to get the IP Address that you want to block, hen head to the .htaccess file and insert the below code to restrict the IP address.

<Limit Get Post>
order allow,deny
allow from all
deny from xx.xx.xx.xx

While inserting your code to your .htaccess file you should replace xx.xx.xx.xx with the IP you like to block, If you like to block more than one IP the insert another “deny from xx.xx.xx.xx” below the first one.

There are ways by which you can use IP addresses to allow or deny requests.

Tip: You can allow are deny the IP address with some variations too. The basic structure of IP is “xx.xx.xx.xx” there these xx range from 0-255. You can use the below combination for an addition functionality with IP addresses.

  • Blocks a specific IP
  • 1.2.3.   Blocks IP from the range to
  • 1.2.      Blocks IP from 1.2.xx.xx
  • 1.2       Blocks IP from 1.2x.xx.xx

You can use these for blocking a single IP to a full range. There is a way by which you can allow only selected IPs, this can be useful if you are making changes to your blog like a new blog design and don’t want anyone to access it till you are finished. This code will allow only you and the IP addresses you want to access your website.

<Limit Get Post>
order deny,allow
deny from all
allow from xx.xx.xx.xx
allow from xx.xx.xx.xx

If you want to add another IP then just simply add another “allow from xx.xx.xx.xx” , remember to replace xx.xx.xx.xx with the IP you want to allow.

Further Read:

Team Zdidit

Team Zdidit take care of all the content creation for zdidit. This team is supervised by Achin Verma

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *