User IP BanningHave a troublesome user who just keeps coming back again and again to cause your community grief? Why not ban them via .htaccess!
## USER IP BANNING
<Limit GET POST>
order allow,deny
deny from 42.12.5.34
deny from 212.173.53.
deny from 69.242.
deny from .aol.com
allow from all
</Limit>
So, what does each one do?
deny from 42.12.5.34 blocks a specific IP address
deny from 212.173.53. blocks all IPs in the 212.173.53.xxx range
deny from 69.242. blocks all IPs in the 69.242.xxx.xxx range
deny from .aol.com blocks everyone from aol.com
Bad Site BanningBut what if you want to ban an entire website from your website, say because they're spamming your statistics or they're just downright bad for some reason.
Not a problem...
## SITE REFERRER BANNING
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} badsite\. [NC,OR]
RewriteCond %{HTTP_REFERER} sub\.badsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} 32\.173\.21\.187 [NC]
RewriteRule .* - [F]
How's it work?
RewriteCond %{HTTP_REFERER} badsite\.com [NC,OR] bans all traffic with badsite.com in the referring URL
RewriteCond %{HTTP_REFERER} badsite\. [NC,OR] bans all traffic with badsite.xxx in the referring URL (covers all TLDs)
RewriteCond %{HTTP_REFERER} sub\.badsite\.com [NC,OR] bans all traffic with sub\.badsite\.com in the referring URL
RewriteCond %{HTTP_REFERER} 32\.173\.21\.187 [NC] bans all traffic with 32.173.21.187 as their referrer
Hotlinking PreventionBut what if you're just wanting to save your bandwidth and prevent people from hotlinking your images? Got you covered...
## DISABLE HOTLINKING
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sub.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?24.56.32.125/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|png|js|css)$ - [F]
# RewriteRule .(gif|jpg|jpeg|png)$ http://www.example.com/images/stealing.gif [R,L]
In short, this one allows gif, jpg, jpeg, png, js and css files to be hotlinked from example.com, sub.example.com and 24.56.32.125 while blocking them from the rest of the world
The last line is optional, basically allows you to serve up whatever image you want when someone links one of your images, to use it uncomment it.