Restricting Access by IP Address and Referer Using .htaccess

Blogged in Web Design, Internet by Matt on Wednesday June 21, 2006

Anyone running a blog or a website that has an open commenting system is sure to be more then familiar with spam. WordPress’s Akismet service works great, however if you have content on your blog or any website that isn’t protected by Akismet you’re bound to be hit by spam.

There are numerous ways to restrict access to your website PHP, .htaccess, mod_rewrite and so forth, however in this example we’re going to use a .htaccess file to deny acccess by IP Address. Below you will find the code to paste into your .htaccess file to restrict access. Whenever a user visits your website from the IP or IP Blocks listed below they will be redirected to your 403 Forbidden page.

order allow,deny

deny from 59.34.113.
deny from 60.176.134.
deny from 60.178.66.
deny from 61.149.45.
allow from all

To make the above function correctly simply copy and paste the above text into your .htaccess file, once updated upload the file to your webserver. How the above code work is as follows; the user connects to your website, .htaccess tells Apache to deny the IP addresses listed in the “deny from xxx.xxx.xxx.xxx” line, once denied the web server send them to your 403 Forbidden page. By adding a full IP range of XXX.XXX.XXX.XXX you block from that IP only, however by adding only XXX.XXX.XXX. you block from that the entire range that follows the XXX.XXX.XXX. . Basically the “blank space” is a wild card. Be careful when you begin blocking large ranges of IP Addresses, as some countries/telecomms may only have a few IP Blocks.

By using the same syntax as the code above you can also block users based by referer. Many spammers crosslink between numerous sites to boost their search engine rankings. So in short to block by referer we use the following code.

order allow,deny

deny from www.nasty.com
deny from .nasty.com
allow from all

Just as before we’ve blocked users coming from the above referer. Before you implement any of these changes be sure you double check all referers as well as IP’s that you plan to block. By making a small typo you can eliminate access to your website all together.

If you’d like a semi-thorough list of IP Addresses that are of confirmed spammers that have got around Akismet, you can click here to get a copy of the current .htaccess I use on all of my websites. Use this at your own discretion!

Popularity: 67% [?]


Leave a Reply

Quote selected text