How to restrict Websites access using .htaccess

There may be times when you notice a certain supicious ip. You belive that the person behind that ip have an intention of harming your website in any way he possibly can, For ex if you have an adsence account he can create Invalid activity by clicking on your ads, resulting you banned adsence account.

Now you must be asking yourself, how to protect your website from that person? One of the easisest method that you can apply is the use of .htaccess file in your website.

How to restrict Websites access using .htaccess

How this method works?

This method have a real simple idea, you have to create a rule in your .htaccess file which will block the ip address from accessing your website. All you have to do is to add that ip in that .htaccess rule.

How to restrict websites access?

Restrict websites access for a  single ip address

order allow,deny
deny from (ip address without brackets)
allow from all

Restrict websites access for Multiple IP addresses

To block multiple IP addresses, add them in this rule one per line

order allow,deny
deny from (ip address without brackets)
deny from (ip address without brackets)
deny from (ip address without brackets)
allow from all

Blocking an entire range of IP

Sometimes you feel a need that entire IP range should be blocked so that the suspicious person won’t be able to access your website while using that range. To block that entire range you will not specify the last octet in your .htaccess file.

Note: Blocking an entire range will also block other genuine user who are using the same range of IP.

order allow,deny

deny from 192.168.0.0

allow from all

Block an IP address using mod_rewrite rules

You can also use mod_rewrite rules as an alternative method to block a specific IP

RewriteEngine on

RewriteCond %{Remote_Addr} ^198\.168\.72\.17$

RewriteRule ^(.*) http://localhost/ [R,L]

To block an entire range using mod_rewrite

RewriteEngine on

RewriteCond %{Remote_Addr} ^198\.168\.72

RewriteRule ^(.*) http://localhost/ [R,L]

Found this article useful follow us On Facebook or Twitter to get More Updates