Forum Moderators: phranque

Message Too Old, No Replies

Blocking IPs

         

missashlay

4:06 am on Nov 25, 2008 (gmt 0)

10+ Year Member



I'd like to block a certain person's IP from being able to view my site, though I can't sort out how to do it. Any help?

phranque

4:32 am on Nov 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], missashlay!

if you have mod_access [httpd.apache.org] installed you can do something like this in your .htaccess file:
order deny,allow
deny from a.b.c.d
allow from all

missashlay

4:37 am on Nov 25, 2008 (gmt 0)

10+ Year Member



Is that the exact thing to use (other than abcd which is obviously the ip)?

phranque

9:58 am on Nov 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



yes with the correct ip that should do it.
just make sure that your target is a fixed ip address and that there aren't others on that ip you shouldn't be excluding.
as always, read the documentation referenced in the above link so you know what each directive means.
you will also need to be allowed to override Limit directives in the .htaccess file by using the AllowOverride [httpd.apache.org] directive in the directory section of your config file, with either:
AllowOverride Limit
or
AllowOverride All

Samizdata

10:30 am on Nov 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



An alternative method is to use mod_rewrite in your root .htaccess:

# Some servers will require this
Options +FollowSymlinks
# Turn on mod_rewrite
RewriteEngine On
# If the undesirable IP
RewriteCond %{REMOTE_ADDR} ^nn\.nn\.nn\.nn$
# Block it
RewriteRule .* - [F]

Replace "nn" with the numbers. The hat (^) symbol means "starts with", the dollar symbol ($) in this context means "ends with", and any line starting with a hash symbol (#) is a comment.

For a list of IPs you put them in sequential lines, with all except the last having an [OR]:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^xx\.xx\.xx\.xx$ [OR]
RewriteCond %{REMOTE_ADDR} ^yy\.yy\.yy\.yy$ [OR]
RewriteCond %{REMOTE_ADDR} ^zz\.zz\.zz\.zz$
RewriteRule .* - [F]

It is also possible to block sequential ranges of IPs in a single line using regular expressions, but .htaccess is extremely powerful and requires absolute accuracy (one character out of place and your site will be offline) so I won't go into that here - there are many examples elsewhere in the forum if you need them.

As phranque said, this is only useful for static IPs, and many people have dynamic ones.

...