Prevent Image Hotlinking With .htaccess and mod_rewrite

By  on  

One way to kill your website's bandwidth and overall download speed is to not block image "hotlinking." What's hotlinking? Hotlinking is linking to a file on an external server that the Web Developer does not own (most of the time the Web Developer doesn't even have permission to use the file). Hot linking occurs mostly with images.

Why would another website hotlink to a file on your server? There are a variety of reasons, both innocent and evil:

  • To use up the other website's bandwidth / save your bandwidth (evil)
  • To link to an oft-changing file so that the file is always up to date on your website (mostly innocent)
  • Laziness (both)

How can this be prevented? Relatively easily using some quick .htaccess directives.

The Code

The following prevents any domain beside yours from hotlinking:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]

The following allows only a friend to hotlink -- everyone else is barred:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?friend1domain.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?friend2domain.com(/)?.*$ [NC]

The following allows for protection of only specified file extensions:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
RewriteRule .*.(gif|jpe?g)$ [F,NC]

The following returns a "stop stealing my images" image:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
RewriteRule .(gif|jpe?g|png|bmp)$ /graphics/stop-stealing.jpg [L,NC]

You can use the above coding practices to prevents thievery of any type of file you'd like, including CSS, JavaScript, and text files.

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My trip to Mozilla Summit 2013 was incredible.  I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out.  MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...

Incredible Demos

  • By
    Disable Autocomplete, Autocapitalize, and Autocorrect

    Mobile and desktop browser vendors do their best to help us not look like idiots by providing us autocomplete, autocorrect, and autocapitalize features.  Unfortunately these features can sometimes get in the way;  we don't always want or need the help they provide.  Luckily most browsers allow...

  • By
    Web Notifications API

    Every UI framework has the same set of widgets which have become almost essential to modern sites: modals, tooltips, button varieties, and notifications.  One problem I find is each site having their own widget colors, styles, and more -- users don't get a consistent experience.  Apparently the...

Discussion

  1. Nicely done!

  2. Brill, I appreciate this may be an old post, but its the answer I needed to fix my lil hotlinking problem so I thought I should say ta …

    TA!!

  3. Perhaps you could help. I’m trying to simplify for some of my clients moving to a cdn platform. for dev I’m using google app engine, just becasue, but files on the prod cdn will be served up the same way. Issue – we would to use .htaccess to have css|js|gif|jpg etc.. files be taken from the cdn by rewrite of the request, so we don’t need to change all the php and related files.

    IE.

    page is rendered with like to image http://example.com/images/image.gif

    but the image should be served from http://example-cdn.com/images/image.gif

    the folder sturcture could remain the same, just the domain would change from example.com to example-cdn.com

    Any idea’s?

  4. Jorge

    Hi, I’m looking at a solution like the one that replaces the images with a generic one (i.e. “stop stealing my images”).
    Can you clarify why it’s not going into an infinite loop? since it’s redirecting to *another* .jpg file -wouldn’t this create infinite redirects to the same image? –thanks!

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?yourwebdomain.com(/)?.*$ [NC]
    RewriteRule .(gif|jpe?g|png|bmp)$ /graphics/stop-stealing.jpg [L,NC]
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!