Prevent hotlinking with a .htaccess file?

by alex93
22 replies
Hi guys, i am about to block 980 sites which are basically robbing me images, having looked all over the web, iv found 3 examples on how to do it, trouble is, iv no idea which one is correct, as some state \.com within them, and some offer different coding styles.

Image of 3 examples from 3 sites, notice the \.com, some have .com/, some also use dollar signs, iv got different types of sites to block, like .tk .com. org. and so on, would love some help so i can get the ball rolling as i is going to take a while.




Cheers for any help you can offer, really stuck and no clear definition found online.
#file #hotlinking #htaccess #prevent
  • Profile picture of the author David V
    Rather than draw this out with a long explanation, it would be worthwhile to read Chris's article at Ultimate htaccess Anti-Hotlinking Strategy | Perishable Press

    Make sure to read the comments as well.

    Some great info.
    {{ DiscussionBoard.errors[9975850].message }}
    • Profile picture of the author alex93
      Originally Posted by David V View Post


      Make sure to read the comments as well.

      Some great info.
      Hi David, iv had a good read of this before, trouble was it simply went straight over my head, I am no coder you see, does anyone have an example of what they use, looking to block entire domains only, not looking to use allow, so hoping that would simplify this process.

      Scratches head, must of spent 4 hours reading and non the wiser.
      {{ DiscussionBoard.errors[9976652].message }}
      • Profile picture of the author alex93
        Hi, can someone validate if this is ok, going to block all image sites except google images.

        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png)$ [NC]
        RewriteCond %{HTTP_REFERER} !^$
        RewriteCond %{HTTP_REFERER} !reviewanygame\.com [NC]
        RewriteCond %{HTTP_REFERER} !google\. [NC]
        RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
        RewriteRule (.*) /index.php?/$1 [R,NC,L]
        {{ DiscussionBoard.errors[9985853].message }}
        • Profile picture of the author Karen Blundell
          for whatever site you want to allow hotlinking the code should look like this:

          Code:
          RewriteCond %{HTTP_REFERER} !^http://www.warriorforum.com/.*$      [NC]
          RewriteCond %{HTTP_REFERER} !^http://www.warriorforum.com$      [NC]
          and your last line should contain the code showing the extensions of the files you want to prevent from being hotlinked by all other sites like so:
          Code:
          RewriteRule .*.(jpg|jpeg|gif|png|bmp|swf|zip|mp3)$ - [F,NC]
          Signature
          ---------------
          {{ DiscussionBoard.errors[9988478].message }}
          • Profile picture of the author alex93
            Originally Posted by Karen Blundell View Post

            for whatever site you want to allow hotlinking the code should look like this:

            Code:
            RewriteCond %{HTTP_REFERER} !^http://www.warriorforum.com/.*$      [NC]
            RewriteCond %{HTTP_REFERER} !^http://www.warriorforum.com$      [NC]
            and your last line should contain the code showing the extensions of the files you want to prevent from being hotlinked by all other sites like so:
            Code:
            RewriteRule .*.(jpg|jpeg|gif|png|bmp|swf|zip|mp3)$ - [F,NC]
            Hi Karen, so i only add this bit then and not the rewrite engine on bit at the start?

            RewriteCond %{HTTP_REFERER} !^http://reviewanygame.com/.*$ [NC]
            RewriteCond %{HTTP_REFERER} !^http://www.warriorforum.com$ [NC]
            RewriteRule .*.(jpg|jpeg|gif|png|bmp|swf|zip|mp3)$ - [F,NC]

            Is there no software online that can do this code for me, i simply want to allow Google to index my jpg images, as they are all jpg, and block all other sites, it seems like a complete nightmare trying to get a bit of code online, feels like i am pulling teeth.

            Because i am messing with an important part of the site, i dont want to cock it up and end up blocking my own images, this is why i am concerned about getting it right.

            Can someone write the 3 lines of code for me, i added this before but nobody seems to be able to verify it? I think i need to add jpg to one line of code and the rest is fine, but unsure?

            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png)$ [NC]
            RewriteCond %{HTTP_REFERER} !^$
            RewriteCond %{HTTP_REFERER} !reviewanygame\.com [NC]
            RewriteCond %{HTTP_REFERER} !google\. [NC]
            RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
            RewriteRule (.*) /index.php?/$1 [R,NC,L]
            {{ DiscussionBoard.errors[9990033].message }}
  • Profile picture of the author yukon
    Banned
    Here's what I did on one of my sites where other domains kept hotlinking my images.

    Watermark the hotlinked images with your domain URL, it's free advertising.

    If you go back & edit the old images to include a watermark, make sure you keep the old URLs intact, that literally gives you 100% control over the images on the domains you don't own.

    Or swap out the old image URLs with banner ads If you want to piss off the guys scraping your content. I would make sure to watermark all your images first because they might come back & just download/host the images on their own host.

    Anyways, your in a good position If someone is hotlinking images. Use it to your advantage (free advertising).
    Signature
    Hi
    {{ DiscussionBoard.errors[9988493].message }}
    • Profile picture of the author Karen Blundell
      Originally Posted by yukon View Post

      Here's what I did on one of my sites where other domains kept hotlinking my images.

      Watermark the hotlinked images with your domain URL, it's free advertising.
      it's a good idea, Yukon - but hotlinking also uses up bandwidth, and on a busy site, unless the person has lots of bandwidth included with their hosting, it can be costly.
      Signature
      ---------------
      {{ DiscussionBoard.errors[9988504].message }}
      • Profile picture of the author yukon
        Banned
        Originally Posted by Karen Blundell View Post

        it's a good idea, Yukon - but hotlinking also uses up bandwidth, and on a busy site, unless the person has lots of bandwidth included with their hosting, it can be costly.
        True hotlinking will use bandwidth but you could always look at the server logs on the host & see which of the hotlinked images are the most popular, then host those few images on an external free host (ex: Google Picasa) & 301/302 redirect the old image URL to the new external host image URL.

        That still gives you free advertising on domains that are hotlinking & you can go back to Picasa or whatever external image host & edit the images for years If needed.
        Signature
        Hi
        {{ DiscussionBoard.errors[9993061].message }}
        • Profile picture of the author alex93
          Originally Posted by yukon View Post


          That still gives you free advertising on domains that are hotlinking & you can go back to Picasa or whatever external image host & edit the images for years If needed.
          Would it be possible to include an alternative image url from say Photobucket, stored on someone else's domain and show my image advert which is clickable, almost like an advertising banner.

          Seeming how there is now 1000 sites, i figured it would be useful to not only put the image advert, but put a clickable image which sends them to the homepage of my site?

          Granted i understand you can place alternative image url, but is it possible to be able to make the image clickable like an advert, like google adsense does?
          {{ DiscussionBoard.errors[9997692].message }}
    • Profile picture of the author alex93
      Originally Posted by yukon View Post

      Here's what I did on one of my sites where other domains kept hotlinking my images.

      Watermark the hotlinked images with your domain URL, it's free advertising.

      Sounds like a plan, i may add an image promoting my site, seeming how over 1000 sites are pinching them, might give me some traffic.cheers.

      Alex
      {{ DiscussionBoard.errors[9990036].message }}
      • Profile picture of the author Karen Blundell
        Hi Alex,

        If you only want to give Google permission to hotlink your images and no other sites the code would look like this:
        Code:
        RewriteEngine On
        
        RewriteCond %{HTTP_REFERER} !^http://google.com/.*$      [NC]
        RewriteCond %{HTTP_REFERER} !^http://google.com$      [NC]
        RewriteRule .*.(jpg|jpeg|gif|png|bmp|swf|zip|mp3)$ - [F,NC]
        you can remove the zip and mp3 extensions if you don't need them -
        Signature
        ---------------
        {{ DiscussionBoard.errors[9990555].message }}
        • Profile picture of the author alex93
          Originally Posted by Karen Blundell View Post

          Hi Alex,

          If you only want to give Google permission to hotlink your images and no other sites the code would look like this:
          Code:
          RewriteEngine On
          
          RewriteCond %{HTTP_REFERER} !^http://google.com/.*$      [NC]
          RewriteCond %{HTTP_REFERER} !^http://google.com$      [NC]
          RewriteRule .*.(jpg|jpeg|gif|png|bmp|swf|zip|mp3)$ - [F,NC]
          you can remove the zip and mp3 extensions if you don't need them -
          Thanks for this, much appreciated, I take it i also need to include my own domain, as most i see include their own domain, wondered if that was standard practice.

          This is the one i pulled online, they also said the jpg must be written as j?peg, iv bolded the potential problem below. Want to allow all search engines so hoping i can use the below and change yourdomain to my domain name.

          RewriteEngine on
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yourdomain\.com/ [NC]
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?google\.(.+)/ [NC]
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(.*\.)?google\.(.+)/ [NC]
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?bing\.(.+)/ [NC]
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(.*\.)?bing\.(.+)/ [NC]
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yahoo\.(.+)/ [NC]
          RewriteCond %{HTTP_REFERER} !^http://(.+\.)?(.*\.)?yahoo\.(.+)/ [NC]
          RewriteCond %{HTTP_REFERER} !^$
          RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

          Just wondered why they said the j?peg must be j?peg and not jpg, kinda confusing.

          Cheers for all the help, i know i am a pain in the arse.
          {{ DiscussionBoard.errors[9992181].message }}
  • Profile picture of the author emptee
    Hi Alex,

    Nope - definitely not.. well, ignoring possible SVG javascript exploits anyway... And you definitely DO NOT want to head down that route!

    302 it to a small PNG or GIF with your url prominently placed.. should get some attention (hopefully!)

    Michael
    {{ DiscussionBoard.errors[9997698].message }}
    • Profile picture of the author alex93
      Originally Posted by emptee View Post

      Hi Alex,

      Nope - definitely not.. well, ignoring possible SVG javascript exploits anyway... And you definitely DO NOT want to head down that route!

      302 it to a small PNG or GIF with your url prominently placed.. should get some attention (hopefully!)

      Michael
      Much appreciated for this, i was concerned.

      Iv finally mastered seo, it has **** all do with content, i have over 20,000 game reviews, well written and no real traffic, it makes me wonder if my money should of been spent on baclinks. Google claims all this shit but from what iv seen is smoke and mirrors, reallly disappointed. Content is king, probably the biggest myth of them all.

      Lets just give all traffic to the person with the most backlinks who has the biggest wallet, what a search engine, iv worked my absolute arse off building my site focusing on the quality, yet i should of just thought, sod it, just buy links instead.

      Sophisticated = Google, that has to be the biggest joke of them all.

      Quality content is king - absolute bullshit.
      .
      {{ DiscussionBoard.errors[10006778].message }}
    • Profile picture of the author alex93
      Originally Posted by emptee View Post

      Hi Alex,

      Nope - definitely not.. well, ignoring possible SVG javascript exploits anyway... And you definitely DO NOT want to head down that route!

      Michael
      Bummeer,, will do what we can lol, going tto be image hotlilinking try and turn the tables, not gettting tot far, how do we piss them off fior good?.
      {{ DiscussionBoard.errors[10006997].message }}
  • Profile picture of the author rhinocl
    If you have cpanel, you can prevent all hot-linking simply by choosing that option there.
    {{ DiscussionBoard.errors[10004547].message }}
  • Profile picture of the author alex93
    Sorry for the added rant, i guess i am quite gullible, i actually thought it was fair game.
    {{ DiscussionBoard.errors[10006809].message }}
  • Profile picture of the author nimonogi
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?yourdomain.com/.*$ [NC]
    RewriteRule .(gif|jpg|swf|flv|png)$ http://www.yoursite.com/hotlink.gif [R,L]
    For more security rules visit .htaccess rules to Harden your website
    Signature
    European Web Hosting by Brontobytes
    {{ DiscussionBoard.errors[10021359].message }}

Trending Topics