Using .htaccess to Prevent Images Hotlinking & Saving your Web Hosting Bandwidth

Did you ever noticed that your images are being hotlinked on someone else’s server and consuming your server bandwidth? This is actually is direct linking to a web site’s files (images, video, etc.).without your permission.

It is bad, as it not only stealing our images, it also take advantage of our bandwidths. Since web hosts charge based on the amount of data transferred, bandwidth is an issue.

Hotlinking should be stopped because others undesirable consequences like switcheroo can often occur.

Using control over.htaccess is one of the most efficient way to prevent image hotlinking. Go through the following steps:

Creating & Placing .htaccess in Root (main) directory

Simply open a notepad & save it as .htaccess without any extension behind. If already have the .htaccess uploaded to your server, simply edit it like in step 2.

Editing .htaccess File

Paste the following code in the .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yoursite.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yoursite2.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ http://yoursite.com/other_image.jpe[NC,R,L]

Note: In Last line of the above code the image other_image.jpg is not hotlink protected or your server can go into an endless loop.

In the above code yoursite2.com can used be stop hotlinking from specific outside domains only, such as myspace.com, blogger.com and livejournal.com

If you want to display 403 Forbidden error on hotlinking simply paste the following code instead of the code shown above.

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

Remember, Hotlinked images can only be replaced by other images, not with an HTML page.

Using .htaccess to prevent hotlinking helps you block some legitimate traffic using these techniques.

What do you think about other ways to prevent images hotlinking on your blogs? Do you like this technique? Share with us your views.

Related Posts with Thumbnails
  1. Hi,
    I found this article looking for protect hotlinking on the net.
    Maybe it is useful to know for peoples who look for hotlinking protection, because, in the case of video files, the htaccess protection by referer didn`t work in my case.
    A good method to prevent hotlinking video and audio is using htaccess and php that redirect to the file on the server, like it is presented in this tutorial: http://www.coursesweb.net/php-mysql/prevent-hotlinking-block-external-access-video-audio-files_t

  2. Thanks for the your insights, CoursesWeb.

Speak Your Mind


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>