Pages

Monday, November 8, 2010

Anti-Leech

If you've ever gone through your site logs and found someone hotlinking to your image files, stealing your bandwidth, then you need the following lines in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/.*$ [NC]
RewriteRule .*\.(gif|GIF|jpg|JPG|bmp|BMP)$ - [F]


Change "yourdomain.com" to your own URL, and upload the .htaccess file to the directory containing your images. Once it is in place, only people coming from "yourdomain.com" will be able to view the images; everyone else will only see a broken image placeholder. If you have an image you'd like to display instead of the placeholder, use the version below.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://yourdomain.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/ [NC]
RewriteCond %{REQUEST_URI} !^/theif.gif [NC]
RewriteRule \.(gif|GIF|jpg|JPG)$ http://yourdomain.com/theif.gif [R]


"theif.gif" is the image you want to load whenever someone attempts to hotlink an image from your server. You only need to add the RewriteCond %{REQUEST_URI} !^/theif.gif [NC] line if the image you want to load is in a directory that the .htaccess file effects. Otherwise you can remove the line, and it will still function.

0 Comments:

Post a Comment