Efficient Method for Preventing Hotlinking via .htaccess

Posted by Michael Robinson on Stack Overflow See other posts from Stack Overflow or by Michael Robinson
Published on 2010-05-24T06:02:37Z Indexed on 2010/05/24 10:11 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

I need to confirm something before I go accuse someone of ... well I'd rather not say.

The problem:

We allow users to upload images and embed them within text on our site. In the past we allowed users to hotlink to our images as well, but due to server load we unfortunately had to stop this.

Current "solution":

The method the programmer used to solve our "too many connections" issue was to rename the file that receives and processes image requests (image_request.php) to image_request2.php, and replace the contents of the original with

<?php
header("HTTP/1.1 500 Internal Server Error") ;
?>

Obviously this has caused all images with their src attribute pointing to the original image_request.php to be broken, and is also the wrong code to be sending in this case.

Proposed solution:

I feel a more elegant solution would be:

In .htaccess

  1. If the request is for image_request.php
  2. Check referrer
  3. If referrer is not our site, send the appropriate header
  4. If referrer is our site, proceed to image_request.php and process image request

What I would like to know is:

Compared to simply returning a 500 for each request to image_request.php:

How much more load would be incurred if we were to use my proposed alternative solution outlined above?

Is there a better way to do this?

Our main concern is that the site stays up. I am not willing to agree that breaking all internally linked images is the best / only way to solve this. I refuse to tell our users that because of something WE changed they must now manually change the embed code in all their previously uploaded content.

© Stack Overflow or respective owner

Related posts about php

Related posts about .htaccess