How to Prevent Image Bandwidth Theft With .htaccess

Protect your images from being linked by other websites while you pay the bandwidth!

How to Prevent Image Bandwidth Theft With .htaccess

by Christopher Heng,

If your website displays beautiful pictures, you may encounter the ugly situation where your photos and other images are used without your permission on other sites. Even worse, those websites "hotlink" your pictures, that is, they don't host the pictures directly on their site, but embed them into their pages by linking to the copy on your site. This way, not only do they infringe your copyright, but they also make you pay for their bandwidth.

System Requirements

The solution outlined in this article requires your site to be hosted on a machine using the Apache web server. In addition, your web host must allow you to override the server's configuration using a .htaccess file. For the more technically inclined, it uses the facilities provided in the mod_setenvif Apache module.

If this is not the case for your website, you cannot use the suggestions given here. You might wish to use my alternative solution, a PHP script that block image bandwidth thieves instead. The article may be found at

(To find out if your web server fulfills the requirements stated here, try checking up the documentation on your web host's website — the information is usually available on their list of web hosting packages, price lists or on their order form. Alternatively, contact their technical support and find out from them.)

Steps to Take

Protecting your images using a .htaccess file is trivial.

  1. Put all the images you wish to protect from being stolen (bandwidth-wise) in a separate directory.

  2. Create an ASCII text file named .htaccess and save it in that directory. Note that the name starts with a fullstop ("period" in US English) and is entirely in small letters (ie, lowercase). Cut and paste the following lines into that file:

    SetEnvIfNoCase Referer "^" locally_linked=1
    SetEnvIfNoCase Referer "^$" locally_linked=1
    SetEnvIfNoCase Referer "^" locally_linked=1
    SetEnvIfNoCase Referer "^$" locally_linked=1
    SetEnvIfNoCase Referer "^$" locally_linked=1
    <FilesMatch "\.(gif|png|jpe?g)$">
      Order Allow,Deny
      Allow from env=locally_linked

    (If you are using Internet Explorer 6, and have trouble selecting the above code to cut and paste, please see this article for a workaround.)

    Change "" to your real domain name. If your site can be accessed using other domain names (eg ""), be sure to add an additional SetEnvIfNoCase line for each of those domain names (with the URLs appropriately changed to the addresses of your domains. On the other hand, if your site can only be accessed using one domain, for example, using only "", then delete the line with "". The cut and paste code above caters to the usual case where most sites can be accessed with or without the "www" prefix.

    Do not correct my spelling in the code snippet given above. "Referer" (with only one "r" in the middle of the word) is the word that needs to go into the .htaccess file — do not change it to "Referrer". (Yes, I know the spelling is wrong in every single variant of English. Unfortunately, that specific sequence of characters is required.)

That's all there is to it. The above file should protect all images that have ".gif", ".png", ".jpg" and ".jpeg" extensions.

Remember to use an ASCII text editor (also known as "text editor" or "plain text editor") to create the .htaccess file. Do not use Microsoft Word or Wordpad. Notepad (found on all Windows systems) is fine.

Explanation: .htaccess to Block Unauthorized Image Usage

Whenever a browser sends your web server a request for an image, it usually also sends the URL of the page that linked to that image. The above .htaccess file causes the server to check this URL (via mention of "Referer" in the above snippet) and if it is one of the authorized URLs that you specify, it will set an internal flag called "locally_linked". This internal flag is technically called an "environmental variable". If the URL sent is not in this list of authorised URLs, the flag (or environment variable) is not set. Note that we also set the "locally_linked" variable if the browser does not send any URL at all: this occurs when the visitor accesses your site using a browser or a proxy that suppresses the referring URL.

The web server then checks if the file requested has an extension in the list given above (gif, png, jpg and jpeg). If so, and the "locally_linked" variable is set, it will send the image. Otherwise it an error will be sent.

What Happens When A Bandwidth Thief Links to Your Image

After you create the .htaccess file, if some other site tries to link to your image from their site, they will find that the image will not display on their site. On the other hand, your images should generally load fine on pages on your site.

Potential Problems

Like the PHP solution, this method relies on the HTTP_REFERER variable (the variable that contains information about the referring page) being properly sent by the visitor's browser. A number of modern browsers as well as some of the anonymous surfing proxies and personal firewalls allow the user to change this header. These browsers or proxies will thus either transmit HTTP_REFERER headers that have some user-specified value or not bother to transmit them at all. There are also buggy browsers around that unpredictably transmit the wrong HTTP_REFERER header on occasion.

When this occurs your visitor will either not view the image even when he is on your site (which means that your own page will have broken link images), or he may be able to view your images even when it is displayed on the copyright infringing thief's site.

Hopefully the percentage of people who encounter this is small, but you have to be aware that these situations do occur.

Copyright Infringement Issues And Alternative Solutions

Besides the issue of paying for some other websites' traffic when a bandwidth thief links to the images on your website, there is also the issue of copyright infringement. When someone uses your proprietary images in order to decorate their pages, that person has actually infringed on your copyright. (You can read more about copyright matters from my article on Copyright Issues Relevant to Webmasters.)

In addition to using the .htaccess file to protect your images, you may also want to send the offending webmaster an email and/or a letter explaining that he/she is violating your copyright and asking him/her to stop the infringing practice. Sometimes that simple message is sufficient to solve the problem.

If that does not work, you can always inform his/her web host of the copyright infringement. Reputable hosts are often very careful about hosting sites which infringe copyright. You will have to furnish proof, of course. If that does not work, you can try complaining to the upstream bandwidth provider. And finally, of course, you can get a lawyer.

I mention the above because sometimes, in the search for a solution to protect their bandwidth, people forget that they have rights that they can legally enforce through other means as well. I suppose this problem of overlooking alternative solutions applies particularly to the more technically savvy people, who tend to look for a software solution to everything even when there might be a simpler approach.

This article can be found at

Copyright 2001-2014 by Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Articles

New Articles

Popular Articles

How to Link to This Page

It will appear on your page as:

How to Prevent Image Bandwidth Theft With .htaccess

Link to Us
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Web Hosting
Perl / CGI
.htaccess / Apache


Free webmasters and programmers resources, scripts and tutorials Free How-To Guides
Site Design Tips at
Find this site useful?
Please link to us.