How To Block Bots, Ban IP Addresses With .htaccess
Got a spambot or scraper constantly showing up in your server logs? Or maybe there’s another site that’s leeching all your bandwidth? Perhaps you just want to ban a user from a certain IP address? In this article, I’ll show you how to use .htaccess to do all of that and more!
Identifying bad bots
So you’ve noticed a certain user-agent keeps showing up in your logs, but you’re not sure what it is, or if you want to ban it? There’s a few ways to find out:
Google it: Try a search like this.
Check the User Agent Database.
Head over to Webmaster World and search again or start a new thread.
Once you’ve determined that the bot is something you want to block, the next step is to add it to your .htaccess file.
Blocking bots with .htaccess
This example, and all of the following examples, can be placed at the bottom of your .htaccess file. If you
don’t already have a file called .htaccess in your site’s root directory, you can create a new one.
#get rid of the bad bot
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^BadBot
RewriteRule ^(.*)$ http://go.away/
So, what does this code do? It’s simple: the above lines tell your webserver to check for any bot whose user-agent string starts with “BadBot”. When it sees a bot that matches, it redirects them to a non-existent site called “go.away”.
Now, that’s great to start with, but what if you want to block more than one bot?
#get rid of bad bots
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^BadBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^EvilScraper [OR]
RewriteCond %{HTTP_USER_AGENT} ^FakeUser
RewriteRule ^(.*)$ http://yoururl.com/goaway.php/
The code above shows the same thing as before, but this time I’m blocking 3 different bots. Note the “[OR]” option after the first two bot names: this lets the server know there’s more in the list.
Blocking Bandwidth Leeches
Say there’s a certain forum that’s always hotlinking your images, and it’s eating up all your bandwidth. You could replace the image with something really gross, but in some countries that might get you sued! The best way to deal with this problem is simply to block the site, like so:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://.*somebadforum\.com [NC]
RewriteRule .* - [F]
This code will return a 403 Forbidden error to anyone trying to hotlink your images on somebadforum.com. The end result: users on that site will see a broken image, and your bandwidth is no longer being stolen.
Here’s the code for blocking more than one site:
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://.*somebadforum\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://.*example\.com [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://.*lastexample\.com [NC]
RewriteRule .* - [F]
If you want to block hotlinking completely, so that no one can hotlink your files, take a look at my article on using .htaccess to block hotlinkers.
Banning An IP Address
Sometimes you just don’t want a certain person (or bot) accessing your website at all. One simple way to block them is to ban their IP address:
order allow,deny
deny from 192.168.44.201
deny from 224.39.163.12
deny from 172.16.7.92
allow from all
The example above shows how to block 3 different IP addresses. Sometimes you might want to block a whole range of IP addresses:
order allow,deny
deny from 192.168.
deny from 10.0.0.
allow from all
The above code will block any IP address starting with “192.168.” or “10.0.0.” from accessing your site.
Finally, here’s the code to block any specific ISP from getting access:
order allow,deny
deny from some-evil-isp.com
deny from subdomain.another-evil-isp.com
allow from all
Done !
Random Post
Related Articles
- Warning hack password Yahoo mail !
- Mechanisms against Spam Mail
- IPSEC - Public Key Infrastructor
- Tutorial: IPSec Security structure
- WordPress enhanced login security plugin
- How to protect login on Wordpress admin ?
- How to surf website by VPN
- Secure Sockets Layer (SSL)
- Secure Sockets Layer (SSL)
- Software for Virtual Private Network
Recent Posts
- Free Panda Internet Security 2010: Windows 7 Launch Party
- FREE 1-year license of Kaspersky Internet Security 2010!
- Warning hack password Yahoo mail !
- How to Use Western Union quick cash at Google Adsense ?
- Western Union available in VietNam
- Facebook for Nokia phones
- DailyMotion now offering Embed HD Video Players
- Free Adword voucher $50
- How to create Shortlinks with Google Apps
- Auto post to Blogger with PHP
- Free Auto Blogger: Auto post rss feed
- The Google Analytics API and PHP
- Using the Google Analytics API - getting total number of page views
- Google Analytics PHP API class
- Blogger team warning Spam posts
- Adsense launching “Category filter Beta”
- Alternatives to Google Adsense by other programs
- What is Adsense Smart Price ?
- How to avoid Adsense Smart Pricing ?
- How to use Adsense again if get banned
Recent Comments
- Patrik
in Warning hack password Yahoo mail ! - Nancy
in Auto post to Blogger with PHP - Tony M J
in DailyMotion now offering Embed HD V… - fonfenVak
in Enable Ping track in wordpress - DaiVyCorp - Int…
in My Google AdSense Account Is Disabl… - DaiVyCorp - Int…
in My Google AdSense Account Is Disabl… - AlexAxe
in VPN Protocols - jennefoh
in Make more money: YouTube Videos Com… - GlenStef
in VPN Protocols - DaiVyCorp - Int…
in Why should I use proxy servers ?
Most Commented
- Top 10 ways to boost alexa ranking (4)
- How to Boost Alexa Ranking (4)
- VPN Protocols (4)
- Make more money: YouTube Videos Coming to AdSense (2)
- My Google AdSense Account Is Disabled (2)
- Top 10 secrets success with business online (1)
- SEO with Site Address / URL (1)
- Free Ways to Increase Your Blog Traffic (1)
- How to SEO wordpress, The Complete Guide (1)
- Manual Unzip Server Command (1)
Most Viewed Post
- Top 10 ways to boost alexa ranking - 1,582 views
- Invalid Clicks Contact Form: How to contact to Google Adsense support Team - 1,184 views
- Adsense launching “Category filter Beta” - 1,177 views
- Auto post to Blogger with PHP - 1,146 views
- How to SEO Copywrite - 1,111 views
- Tutorial: IPSec Security structure - 1,108 views
- SEO with Site Address / URL - 1,009 views
- Free Auto Blogger: Auto post rss feed - 948 views
- How to Prevent Spam VBB with GeoIPCountry - 883 views
- How to Use Western Union quick cash at Google Adsense ? - 876 views
Categories
- How to (51)
- SEO (33)
- Google adsense (31)
- Business land (13)
- Security Policies (21)
- Tips (26)
- Technology news (7)
- Traffic guide (9)
- Top secrets MMO (6)
- Tutorials (10)










No Comment
Leave Your Comments Below