Hotlink prevention using a simple .htaccess

in Thought, Webdev / tags: , , , ,

This month, I find that this site consumed unusual bandwidth usage. Probably, the traffic is increasing, but still, it looks a little bit strange. So, I checked my bandwidth usage log from webhosting control panel, and found an “interesting” information. I have more than 2.5 Gb of bandwidth usage only for images, only in one week. Okey, it seems that there are some sites stealing my bandwidth usage.

Few months ago, I started placing a small .htaccess inside my image directory. There are some “upload folders” inside my hosting account. Unfortunately, I forgot to put .htaccess in all upload folders. I know, I can do it easily by placing that .htaccess in the main public directory. But, because Movable Type — my current blogging platform — also generates .htaccess file inside the main public directory, I need to do another mechanism.

There are some tricks we can use to avoid hotlink prevention. For more detailed information, you can read an articles at A List Apart. It’s about Smarter Image Hotlinking Prevention. So, what’s in my .htaccess file then?


Here are some lines I put inside .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?orangescale\.net/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?orangescale\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?terasi\.net/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://orangescale.net/nohotlink.png [L]

Those lines mean:

  • Avoid hotlink from other web pages but from these domains: orangescale.net, orangescale.com, and terasi.net
  • If there are web pages try to do hotlinking, the referring image will be replaced by an image located at http://orangescale.net/nohotlink.png.
  • Those rules will apply to all files having .jpg, .jpeg, .bmp, and .png extensions

There are some ‘flags’ there like [NC] and [L]. The [NC] flag makes the statement case insensitive while [L] identifies that the line is the last rule.

Why I do this? Because I do not have super-extra-bandwidth. I pay for the bandwidth. And, I tried not to do hotlink when I insert files in my posts. It is fine for me if images found in my website used by other, as long as they keep those images into their own server. After few days, it seems my bandwidth usage is back to normal.

If you're new here, you may want to subscribe to my RSS feed go get the latest entry from your RSS reader. You can also have my contents delivered to your inbox.

This entry receives 5 comments.

  1. Pujiono

    Paling nyaman memang menempatkan gambar di image hosting. Nggak perlu mikir benwit. Nggak usah takut benwit dicolong. Gampang ketika pindah blog hosting, nggak usah upload gambar satu per satu.

    Tapi cara itu cuma buat blog yang katro spt punyaku.

    Thanks atas informasinya, Thoms…

    Jan 17, 2008 at 10:43 am

  2. Thomas Arie

    Benar Mas, itu bisa jadi solusi. Tapi, kemungkinan terburuk adalah penyedia layanan penyimpanan gambar tersebut menghentikan layanan atau mengubah layanan. Kalau ini mengakibatkan gambar jadi tidak bisa diakses, repot juga kali ya… :)

    Jan 17, 2008 at 2:01 pm

  3. Pujiono

    Betul juga sih. Gimana kalo misalnya penyedia jasa image hosting itu tutup? Wah, pasti aku akan kebingungan. Aku pernah merasakannya waktu tahu kalo WebShots.com akan dijual oleh induk perusahaannya. Padahal khan AllYouCanUpload yang selama ini saya pakai adalah anaknya WebShots. Duh, jangan-2 mereka bangkrut, pikirku.

    Untunglah yang beli WebShots adalah AmericanGreeting, sebuah perusahaan yang sudah cukup lama berkecimpung di dunia images.

    Sama juga ketika Photobucket.com yang pernah diblok oleh MySpace. PhotoBucket kelimpungan. Eh, ujung-ujungnya PB malah dibeli sama MS (bukan mikrosop, tapi MySpace).

    Jadi, kalo mau nitip gambar, ada baiknya pilih perusahan yang sudah bonafid, jangan perusahaan abal-abal. Ugh, panjang bener komenku..

    Jan 17, 2008 at 2:37 pm

  4. zam

    saya pernah menerapkan teknik ini..

    dan dengan suksesnya saya diunek-unekne karena foto-foto yg nongol di Guugle Reader berubah ke gambar yg saya alihkan itu..

    heheh..

    akhirnya saya kembalikan seperti semula.. :D

    Jan 17, 2008 at 8:48 pm

  5. Thomas Arie

    Pujiono, kalau penyedianya memang memiliki reputasi, saya rasa tidak masalah. Apalagi memang layanan tersebut mengijinkan hotlink. Sama seperti Flickr, kalau memang kebetulan gambar/foto yang ingin saya tampilkan sudah diletakkan di Flickr, saya akan embed langsung saja…

    zam, hehehe… lha memang dialihkan ke gambar apa hayoh! Mungkin web-based RSS reader perlu dimasukkan ke whitelist kali ya…

    Jan 17, 2008 at 10:18 pm

Your feedback, please...