How To Enable Hot Linking Protection In Apache

Question : I have a site with lots of good images and other site enabled hot links to my image from their own site. Due to this it is eating lots of my bandwidth, so how do I stop them to prevent hot link under Apache web server ?

Answer : This happens if your site hosts has unique images. It is very simple to ban image hot linking using Apache mod_rewrite to check the referral info which browser provides.

Prevent Apache Hot Linking Of Images or Media

There are lots of way to block the hot linking of images.

You can add any one of the following code into our .htaacess file or into your Apache httpd.conf file to block hot linking. Before doing anything please make sure Apache mod_rewrite is enabled.

Solution 1: Prevent Hot Linking Of Images

Edit httpd.conf file or .htaccess file in your favourite text editor.

# vim httpd.conf

Now append the following config directive:

SetEnvIfNoCase Referer "^http://www\.looklinux\.com/" banimages=1
SetEnvIfNoCase Referer "^http://looklinux\.com/" banimages=1
SetEnvIfNoCase Referer "^$" banimages=1
<FilesMatch "\.(gif|png|jpe?g)$">
  Order Allow,Deny
  Allow from env=banimages=1

Alternatively , you can also use following simple code:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?looklinux.com/.*$ [NC]
RewriteRule ^.*\.(bmp|tif|gif|jpg|jpeg|jpe|png)$ - [F] 

Solution 2 : Prevent Hot Linking Of Images And Redirect It To New Image

This solution will stop hotlinking and displays alternate images to endusers:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?looklinux\.com/.*$ [NC]
RewriteRule .*\.(gif|jpe?g|png)$ http://www.looklinux.com/null.jpg [R,NC,L]

Now restart the Apache web server to take above effect.

# service httpd restart


Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author


Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

1 Comment

Leave a Comment