How to block access to your website

Web programming topics
Post Reply
Tony
Lieutenant
Lieutenant
Posts: 86
Joined: Tue Jul 21, 2009 4:11 pm

How to block access to your website

Post by Tony » Sun Nov 29, 2009 3:35 am

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

Code: Select all

order deny,allow
deny from 192.11.22.33
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:

Code: Select all

<files image.gif>
order deny,allow
deny from 192.11.22.33
</files>
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:

Code: Select all

ErrorDocument 403 /banned.htm
order deny,allow
deny from 192.11.22.33
deny from 192.77.88
<files banned.htm>
allow from all
</files>
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:

Code: Select all

order allow,deny
allow from 192.11.22.33
More information can be found on these techniques in the Apache documentation here, at the bottom of that page.
Post Reply

Return to “Web programming”