About Linux FAQ

Browse More FAQs:

Apache prevent hot linking or leeching of images using mod_rewrite howto

Posted by Vivek Gite [Last updated: June 12, 2008]

This FAQ is part 2 of 3 in the series Apache

Q. My site hosts lots of good images and other site hot links to my images from their own site. Hot linking is eating lots of my bandwidth. How do I stop lechers or prevent hotlinking under Apache web server?

A. This is problem you may encounter, particularly if your site hosts unique images. However solution is quite simple ban image hot linking using Apache mod_rewrite to check the referral information the browser provides.

How do I prevent Apache hot linking of images / media?

There are many ways to block hot linking of images.

You can add any one of the following code to .htaccess file or to your own httpd.conf file to prevent.

Make sure Apache mod_rewrite is enabled.

Solution # 1 : Prevent "hot linking" of images

Open httpd.conf or .htaccess file using vi text editor
# vi httpd.conf
Append following config directive:

SetEnvIfNoCase Referer "^http://www\.cyberciti\.biz/" banimages=1
SetEnvIfNoCase Referer "^http://cyberciti\.biz/" banimages=1
SetEnvIfNoCase Referer "^$" banimages=1
<FilesMatch "\.(gif|png|jpe?g)$">
  Order Allow,Deny
  Allow from env=banimages=1
</FilesMatch>

Or you can use following simple code:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?cyberciti.biz/.*$ [NC]
RewriteRule ^.*\.(bmp|tif|gif|jpg|jpeg|jpe|png)$ - [F] 

Solution # 2 : Prevent "hot linking" of images and redirect to new image

This method stop hotlinking and displays alternate image to endusers

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?cyberciti\.biz/.*$ [NC]
RewriteRule .*\.(gif|jpe?g|png)$ http://www.cyberciti.biz/noop.jpg [R,NC,L]

Unless the image is displayed on cyberciti.biz, browers would see the image noop.jpg. Replace domain cyberciti.biz and upload noop.jpg to webroot.

If you made changes to httpd.conf file; restart Apache:
# /etc/init.d/httpd restart

Navigation for Apache«Display Apache Server Status with mod_statusApache Web Server Cache Frequently Used Files To Improve Performance»

E-mail this to a friend      Printable version

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Nandkishor Says:

    Hi,
    I have configured the many virual hosts. If many userser login or hit this hosts then some session files with there login number like (sess_76hy679jb a very big number) is created in /tmp directory.
    This files are deleted automaticaly. But currentaly these files are not deleted & they give session error. Where I change the default limit of this session files.
    Give me solution for this.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.