B&T's Tips & Scripts
B&T's Tips & Scripts is a collection of Tips & Scripts to help you build a better website. The Tips & Scripts are targeted towards the native Apache PHP MySQL environment. Check back frequently as there will be additions and modifications. Please read our terms of use and our frequently asked questions. And while you are here, check out B&T's Tool Box.

Blocking traffic to your website

Last updated: January 2, 2005

To block an IP address or a domain name from accessing your website, put these lines in your .htaccess file.

order deny,allow
deny from
deny from badwebsite.com

You can specify a partial IP address (just the first three sets of numbers, for example) if you want to block a range of addresses.

You can also use a partial domain name.

Note that blocking by domain name depends on the browser sending the referrer code, which is blocked by some firewalls and browser configurations.

You can limit the blocking to certain files or directories by putting a <files> or <directory> tag around the code.   When using the <files> tag you can specify a file name or a file type.   See the example below:

<files image.gif>
order deny,allow
deny from

You can expand this to block multiple IPs or domain names and create a special error page.   Create and upload a special page, in this example banned.htm, then use this in .htaccess:

ErrorDocument 403 /banned.htm
order deny,allow
deny from
deny from 192.66.77
<files banned.htm>
allow from all

You can also use this technique to make a private website (or directory) by restricting access by IP address.
Using the .htaccess lines below, only requests from the specified IP address will be allowed:

order allow,deny
allow from

More information can be found on these techniques in the Apache documentation here, at the bottom of that page.
Rate This Topic
topic rating using this script.
You are visitor 1,538,529 (using this script)
Copyright © 2004-2014