Tuesday, August 27, 2013

Stop image hotlinking - lighttpd

Let us say you would like to prevent hotlinking for domain theos.in:
=> Allow all image refer from your own domain theos.in and other domains such as nixcraft.com or cyberciti.biz
=> Allow image bot crawlers/ useragent such as msnbot-media (MSN), Mediapartners-Google (Google) and Yahoo-MMCrawler (Yahoo)
=> Allow image refer from images.google.com, images.search.yahoo.com etc
=> Allow feedburner to burn your feed and images
Open your lighttpd.conf file. Locate virtual domain configuration section for domain theos.in:
# vi /etc/lighttpd/lighttpd.conf
Find virtual domain configuration and append code as follows:
$HTTP["referer"] !~ "^($|http://.*\.(theos\.in|^$|google\.*|yahoo\.*|msn\.*|nixcraft\.com|cyberciti\.biz|cricketnow\.in))" {
$HTTP["useragent"] !~ "msnbot-media" {
$HTTP["useragent"] !~ "Mediapartners-Google" {
$HTTP["useragent"] !~ "Yahoo-MMCrawler" {
$HTTP["useragent"] !~ "FeedBurner" {
url.access-deny = ( ".jpg", ".jpeg", ".png", ".gif", ".avg", ".mpeg" )
}
}
}
}
}

Save and close the file. Restart lighttpd webserver:
# /etc/init.d/lighttpd restart
Final configuration includes support for MSN, Google, Yahoo cache :)
$HTTP["referer"] !~ "^($|http://.*\.(google\.*|yahoo\.*|msn\.*|cyberciti\.biz|msnscache\.com/.*))" {
$HTTP["referer"] !~ "^($|http://theos\.in|^$|nixcraft\.com|cricketnow\.in)" {
  $HTTP["useragent"] !~ "msnbot-media" {
  $HTTP["useragent"] !~ "Mediapartners-Google" {
  $HTTP["useragent"] !~ "Yahoo-MMCrawler" {
  $HTTP["useragent"] !~ "FeedBurner" {
  $HTTP["referer"] !~ "^($|http://.*/.*(q=cache.*|p=cache.*))" {
     url.access-deny = ( ".jpg", ".jpeg", ".png", ".gif", ".ico" )
       }
      }
     }
    }
   }
  }
 }
}
It can get more complicated but above is sufficient for most websites ;)

No comments:

Post a Comment