Hotlink protection

Discussion in 'Site & Server Administration' started by Crusader, Oct 19, 2005.

  1. #1
    I'm noticing more and more people hotlinking to images on my wallpapers site for use as backgrounds for websites. Now with files averaging 300KB this eats up quite a lot of my bandwidth.

    The server I'm using has CPanel. Can someone please tell me how I go about using the hotlink protection? It seems to be enabled, but jpeg files are still being hotlinked to, so I'm guessing I'm doing something wrong.
     
    Crusader, Oct 19, 2005 IP
  2. wrmineo

    wrmineo Peon

    Messages:
    3,087
    Likes Received:
    379
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Someone actually gave me this info when I was having issues, see if it's applicable for your situation and simply modify the url info:


    I see your webserver is running Apache for the isnowlegal.com domain. If your web host has mod_rewrite enabled, and allows you to utilize .htaccess, you could create a rewrite expression that rewrites anything that shows up in the referring string other than your site to a specific image (such as a can of SPAM perhaps lol). In such a case, this would work:

    Code:

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?isnowlegal\.com [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule \.(jpe?g|gif|bmp|png)$ /paris_hilton/images/you-are-eating-my-bandwidth-for-breakfast.gif [L]



    This will allow anyone viewing your picts referred from your site isnowlegal.com and www.isnowlegal.com to view the images. Anything else that appears in the HTTP_REFERER HTTP header will get served up the “you-are-eating-my-bandwidth-for-breakfast.gif” image. It's good to have a small image but something obnoxious enough for them to be motivated to change the link to your image.

    While this mod_rewrite expression is a neat little trick, unfortunatly if you had some good potential visitors that found you on say Google Image search (for example). The large image that would get displayed in the lower frame would not be the desired image and it may appear to some people that your site is messed up or something. An easy workaround for that would be to add expression parameters for that to include images.google.com and images.search.yahoo.com or any other major image search engine that you would like to include - i.e.:

    Code:

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?isnowlegal\.com [NC]
    RewriteCond %{HTTP_REFERER} !^http://images\.google\.com [NC]
    RewriteCond %{HTTP_REFERER} !^http://images\.search\.yahoo\.com [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule \.(jpe?g|gif|bmp|png)$ /paris_hilton/images/you-are-eating-my-bandwidth-for-breakfast.gif [L]



    This way you’re saying it's ok to pull up the images from the website (isnowlegal.com), from images.google.com and images.search.yahoo.com while implicitly denying all other referring URI.

    Even still at some point you probably don't want to even serve them up any images - even dummy ones. If such is the case, simply change the RewriteRule part to this and they will just get returned a 403 forbidden code:

    Code:

    RewriteRule \.(jpe?g|gif|bmp|png)$ - [F]



    Hope it helps... Good luck.
     
    wrmineo, Oct 19, 2005 IP
    exam likes this.
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great post wrmineo! I'll save it for when I need it.
     
    exam, Oct 19, 2005 IP
  4. Crusader

    Crusader Peon

    Messages:
    1,735
    Likes Received:
    104
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks. I figured out how CPanel works.
    You enable hotlink protection
    Enter URL's of sites you want to allow (both with http://www.domain.com and http://domain.com)
    In block for "Extensions to allow (seperate by commas)" you enter: jpg,jpeg,bmp,gif (Basically all image formats)
    And click on activate and you are set.

    The thing is "Extensions to allow" is actually the extensions that's not allowed. Weird I know.

    The result: Some sweet revenge on bandwidth thieves http:// members2.blackplanet.com/stylishgent /
    Wonder how long it will take for them to figure out that their white text isn't showing due to the black background being gone.
     
    Crusader, Oct 19, 2005 IP
  5. wrmineo

    wrmineo Peon

    Messages:
    3,087
    Likes Received:
    379
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Someone sent me information specific to cPanel but I can't find it. I don't have cPanel so I may have trashed it - more likely it's just lost in the shuffle - I rarely throw anything a way, it would screw up my disorganized disorder. Anyway, I'll keep looking because the code they sent me had some good "allows" for "acceptable" urls like Google Images etc. and also had to "swap" the image with an alternate one.
     
    wrmineo, Oct 19, 2005 IP