How To Avoid Image Hotlinking & Bandwidth Theft In Your Website

Image Hotlinking is a common cause of increase in Bandwidth utilization of a website. Bandwidth theft or “hotlinking” is direct linking to a web site’s files (images, video, etc.). An example would be using an <img> tag to display a JPEG image you found on someone else’s web page so it will appear on your own site, eBay auction listing, weblog, forum message post, etc. Bandwidth is expensive so a webmaster has to manage it carefully. One of the way to optimize the bandwidth usage is by compressing the website output. Also there are times when people will hotlink to an image hosted on your server from their blog/website which results in increase in bandwidth. For example:
<img src="http://yourwebsite.com/blog/folder/someimage.jpg"></img>
Code language: HTML, XML (xml)
Thus, for every page request for that blog, a request will be generated for this image to your server. This will eat up the bandwidth and also may increase CPU time. So how to stop hotlinking? Well just add following code in your .htaccess file: Your site url is www.mysite.com. To stop hotlinking of your images from other sites and display a replacement image called nohotlink.jpg placed in your images directory, place this code in your .htaccess file:
#Stop Image Hotlinking RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !google. [NC] RewriteCond %{HTTP_REFERER} !search?q=cache [NC] RewriteCond %{HTTP_REFERER} !msn. [NC] RewriteCond %{HTTP_REFERER} !yahoo. [NC] RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpe [L]
Code language: HTML, XML (xml)
The first line of the above code begins the rewrite. The second line matches any requests from your own mysite.com url. The [NC] code means “No Case”, meaning match the url regardless of being in upper or lower case letters. The third line means allow empty referrals. Then we have few lines to Allow Search Engines like Google, Yahoo, MSN etc to crawl the images.The last line matches any files ending with the extension jpeg, jpg, gif, bmp, or png. This is then replaced by the nohotlink.jpe file in your images directory. This JPEG image is using the extension jpe instead of jpg to prevent blocking your own replacement image. Before uploading your .htaccess file to your server make sure that there is not one there already! Applications such as WordPress use their own .htaccess file when installed, if this is the case then download the existing .htaccess file, add your extra code and then upload it back to the server.

View Comments

  • Hi,

    i would like to use your example of the .htaccess and I have one question.
    My images are in de directory e2/gal_1/images/, e2/gal_2/images/ and so on.

    Can I repeat the statement:
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ e2/gal_1 /images/nohotlink.jpe [L]
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ e2/gal_2 /images/nohotlink.jpe [L]
    ......
    en so on?

    Respect & regards

    Franco

  • Dear Viral
    How do i prevent direct access to the Images and PDF documents
    Using Apache with Perl cgi
    Regards
    Roy

  • h viralpateli,
    can you give info on above topic that if i want to allow hotlinking from certain ips or websites then what will be the code

Share
Published by
Viral Patel
Tags: htaccess Website optimization

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago