iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 13, 2008
MySpace Profile Page Resources
HTML Goodies
 
May 13, 2008
How to Upload Your Photos onto the Web
HTML Goodies
 
May 13, 2008
Email Marketing for MySpace Artists
HTML Goodies
 
May 13, 2008
Top Online Marketing Techniques
HTML Goodies
 
May 13, 2008
I want to create a site just like ____, is that a violation of...
About
 
May 12, 2008
Film Makers, Bands and Comedians Welcome on MySpace
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /SSI

Prevent Hotlinking With htaccess 

  Views:    14073
  Votes:    10
by Joni Carlton 11/07/04 Rating: 

Synopsis:

One of the most irritating things to find, is someone linking to your images, and running up your bandwidth. Stop it with htaccess.
Pages: 
The Article

One of the most irritating things to find, is someone linking to your images, and running up your bandwidth.

You go through your web statistics and find a website that has not only stolen your images, but in fact simply linked right to them, on your server.

The problem with this is not just the theft of your images, but it runs up your bandwidth as well.  This is known as "Leeching".

A few options to choose from
When using htaccess to protect your images, you have a few options.  These options determin who can and who cannot link to your images.

I will cover a few of them here.

Creating the htaccess file (some things to remember)
A few things to remember about htaccess, is that any folder you place the htaccess file in, it will affect that folder, and any folder beneath it.

The first thing you will want to do is open a text editor, I prefer Crimson Editor or Notepad.

The first basic bit of code will be used to turn on mod_rewrite, and tell the server which domains will be allowed to link to your files.

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

Line one turns on the mod_rewrite engine.  The second and third line is telling the server to look for referrer strings that are not left blank.  The third line is telling the server which domains are allowed to link to your files.

If you want to allow more than your domain to link to your files, you can add them by adding more RewriteCond lines.  For example:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?seconddomain.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?thirddomain.com(/)?.*$ [NC]

Which file extensions do you want to protect?
The following code, added to the bottom of the above code, will tell the server which file extensions you want to protect.  Using the the F and NC flags basically tell the server that it's Forbidden from accessing the files, and the NC flag means No Case, telling the server to ignore the case (i.e. bmp and BMP will be treated the same):

RewriteRule .*\.(gif|jpe?g|png|bmp)$ [F,NC]

The completed code
When you compile al of this code thus far, you will have enough to protect your images, and bandwidth:

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

Displaying an image of your choice
Keep in mind that this option will not protect your bandwidth, but more so allow you to control it better, as well as display a humiliating image on the leecher's site.

The RewriteRule that we used above, to determine the file extensions you want to protect, can be added to, to display your own image on the leecher's site.  You can change the line to:

RewriteRule \.(gif|jpe?g|png|bmp)$ /images/humiliatingimage.gif [L,NC]

Like I said however, this will not save your bandwidth as it is still displaying the "humiliating" image from your server.

The completed code would then look like this:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com(/)?.*$ [NC]
RewriteRule \.(gif|jpe?g|png|bmp)$ /images/humiliatingimage.gif [L,NC]

Saving your file
Now save your htaccess file as .htaccess.  Make sure there is no .txt extension on it, as this will not function properly.

Upload the htaccess file to the folder of your choice, and there you have it!

Pages: 

Similar/related articles:


 
  Sponsors