firewall Spyware removal internet speed test web security service free software and tips


Go Back   Web Security > Internet Security > Computer Security
FAQ Members List Search Today's Posts Mark Forums Read

Computer Security Covers issues surrounding security such as firewalls, intrusion detection systems, badware, browser security and more.

Advertisements

How do I block an IP address from accessing my web site?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-2008,
Junior Member
 
Join Date: Mar 2008
Posts: 1
Default How do I block an IP address from accessing my web site?

How do I block an IP address to prevent a specific individual from accessing my web site?

Jm
Reply With Quote
  #2 (permalink)  
Old 03-10-2008,
AMPC's Avatar
Administrator
 
Join Date: Jan 2007
Posts: 1,415
Default Block an IP Address using .htaccess

Welcome to the forum Jm!

Here is how to block an ip (computer) from entering your site using the .htaccess file. In this example, I'll use 209.172.44.78:

Create a file in the root of your website (main page of your website) and call it .htaccess

Inside that file, you'll add this code:

Quote:
order allow,deny
deny from 209.172.44.78
allow from all
Save the file and that's it! Any requests (get and post) by 209.172.44.78 will be blocked.

I'm sure that over time, you'll want to block more than one IP address in your htaccess file, so here is how to block more than one. I'll use 72.232.162.50 in addition to the existing address as an example.

Quote:
order allow,deny
deny from 209.172.44.78
deny from 72.232.162.50
allow from all
You simply add another deny statement for each additional IP address you want to block.

Say that you are getting blasted by many ip address in the same range (block), such as:

71.32.62.51
71.32.62.52
71.32.62.53
71.32.62.54
71.32.62.55
71.32.62.56
71.32.62.57

You can add the following line to your .htaccess file:

Quote:
deny from 71.32.62
This tells your server to block any address that starts with 71.32.62 (we simply left off the last octet).

and here is what the entire .htaccess file would look like:

Quote:
order allow,deny
deny from 209.172.44.78
deny from 72.232.162.50
deny from 71.32.62
allow from all

The .htaccess code above will block access for IP address 209.172.44.78, 72.232.162.50 and any IP address ranging from 71.32.62.0 to 71.32.62.255

Let's take it even further - say that you're getting blasted by a ton of IP addresses that don't have a pattern but are from the same ISP / domain (such as hotlinker.com). The solution is to simply block all traffic from the ISP using the following command (provided hostnameLookups are enabled on your server):

Quote:
deny from hotlinker.com
This prevents any ip address from hotlinker.com from accessing your site - just stick that deny right under the last deny statement.

Always use caution when blocking an entire range or ISP using your .htaccess file, you may end up blocking valid visitors by accident.

Hope that helps!

Best regards,

Jim.
Reply With Quote
  #3 (permalink)  
Old 08-11-2008,
Junior Member
 
Join Date: Aug 2008
Posts: 5
Default

Nice tutorial. Question how do you set it up to only allow access from a certain IP range. Basically I could force only my clients to allow access to the private backend of site.
Reply With Quote
  #4 (permalink)  
Old 08-11-2008,
AMPC's Avatar
Administrator
 
Join Date: Jan 2007
Posts: 1,415
Default Block everyone except IP htaccess

Deny everyone access, then allow certain hosts/IP addresses

ErrorDocument 403 /GoAway.html
<Limit GET POST>
order deny,allow
deny from all
# Allow all IP's starting with 21.22.23.
allow from 21.22.23.
# Allow access from Google.com
allow from .google.com
</Limit>


For referrers, use this:

Block traffic from competitors site / case insensitive

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} competitorsite\.com [NC,OR]
RewriteRule .* - [F]

Uncomment the line with "Options +FollowSymlinks" above (remove the #) if your server is not configured with FollowSymLinks in its <directory> section of the httpd.conf, and you get a 500 Internal Server error when using the code above as is.

Let me know if that works for you!

Best regards,

Jim.
Reply With Quote
  #5 (permalink)  
Old 09-01-2008,
Junior Member
 
Join Date: Sep 2008
Posts: 1
Default

Quote:
Originally Posted by AMPC View Post
Block traffic from competitors site / case insensitive

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} competitorsite\.com [NC,OR]
RewriteRule .* - [F]

Uncomment the line with "Options +FollowSymlinks" above (remove the #) if your server is not configured with FollowSymLinks in its <directory> section of the httpd.conf, and you get a 500 Internal Server error when using the code above as is.
Hi,

What is the code to block several sites?

Thanks!
Reply With Quote
  #6 (permalink)  
Old 09-11-2008,
AMPC's Avatar
Administrator
 
Join Date: Jan 2007
Posts: 1,415
Default blocking a range of IP addresses using htaccess

You can always block an entire range, say 21.22.23.x by using:

order allow,deny
deny from 21.22.23
allow from all

You can block ranges using CIDR (Classless Inter-Domain Routing [sounds like 'cider']), so that the command
deny from 21.22.0.0/16
would block everything in the 21.22.x.x range. BTY - the command:
deny from 21.22
does the same thing as the command above it.

Hope that helps.

Regards,

Jim.
Reply With Quote
  #7 (permalink)  
Old 01-01-2009,
Junior Member
 
Join Date: Jan 2009
Location: the World
Posts: 3
Default

this can also be done by using a php script. However the disadvantage of my suggested method is that it will only apply to whatever pages you include the code on. But this can be an advantage, depends on whatever point of view you have. For me it is an advantage and here is the php code you need to use to block an ip using a php script.

PHP Code:
<?
$banned
[0]="xxx.xxx.xxx.xxx"// IP in the form of "192.168.1.1" or whatever
$banned[1]="yyy.yyy.yyy.yyy";

// add as many as you wish

if (in_array($_SERVER['REMOTE_ADDR'],$banned)) header("HTTP/1.1 403 Forbidden");
?>
__________________
-
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is .


Powered by a CPU
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.3.2 © 2009, Crawlability, Inc.