Sarathlal N

Disable image Hotlinking via .htaccess

The Hotlinking is a practice to display image on website by linking to the image on a different server space, rather than keep a copy of that image.

If we hotlinked an image in our web page, When a visitor visit that web page, the image is loaded from another server space & displayed in our web pages.

By the way, we can reduce the total bandwidth of our web server to render our page. But same time we consumed another web server bandwidth to render our page. So hotlinking is normally called as bandwidth theft.

So if you have a website, normally people advise to disable hotlinking from our server because we don’t have any benefit and unauthorized people utilize our web server bandwidth.

In apache web server, we can easily disable hot linking by adding a few lines in our .htaccess file. The .htaccess file is our apache configuration file & normally it is a hidden file.

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

We just want to replace mysite.com/ with our own domain in second line & correct image path & name in fourth line.

With this code snippet, first our web server check the referrer to see that it matches our own URL and it is not empty. If it doesn’t, and the file has a JPG, GIF, BMP or PNG extension, then the nohotlink image (from forth line of code) is displayed instead.

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.