Deny Direct Access To Images From Other Websites In .htaccess

Discussion in 'Apache' started by georgi, Jan 28, 2013.

  1. #1
    Hi everyone,

    I have recently had number of websites that link directly to images from my website. This is not hotlinking, it is direct server request. As an example: on the linking website there is image gallery script with thumbnails and when the visitor clicks on the thumb it calls the image from my website.

    I block their IP-s in .htaccess, but it is not the best way to stop them since IP change. Is there any way, similar to anti-hotlinking, to deny such direct access to my images by domain name i.e. to allow only from my website and deny from all others. Or something else that could work in my case with .htaccess.

    your help is greatly appreciated,
    georgi
     
    Solved! View solution.
    georgi, Jan 28, 2013 IP
  2. #2
    Many years ago, when I had a person blog, I wrote a few articles on blocking image leechers about a few methods, such as .htaccess, custom php scripts (and even combining them).

    I think what you want is this:

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
    RewriteRule \.(jpg|jpeg|png|gif|tif|bmp)$ - [NC,F,L]
    Code (markup):
    Obviously, replacing google.com with your sites name. That's just a guess (I haven't tested it as I'm at work).
     
    ryan_uk, Jan 28, 2013 IP
  3. georgi

    georgi Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    thanks for your time. will try it as soon as possible and will post the result.
     
    georgi, Jan 29, 2013 IP
  4. georgi

    georgi Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #4
    result is bad: the code blocks my images to be displayed in Google image search
     
    georgi, Jan 29, 2013 IP
  5. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #5
    You wrote, "to allow only from my website and deny from all others".

    So, you just need to add a line for Google, Yahoo, Bing, etc, such as:

    RewriteCond %{HTTP_REFERER}  !google\.                      [NC]
    Code (markup):
    Is the simplest way. (Rather than what I originally wrote due to all the different ccTLDs for Google.)
     
    ryan_uk, Jan 29, 2013 IP
  6. georgi

    georgi Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #6
    yes. you are right.
    and what about regular anti-hotlinking? i also have it in .htaccess. are all hotlinking sites also be blocked in this case?
    thank you for your help
     
    georgi, Jan 29, 2013 IP
  7. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #7
    I don't know what you mean by regular anti-hotlinking.

    The above won't allow blank referrers and only allow your own site and Google as the referring sites.
     
    ryan_uk, Jan 29, 2013 IP
  8. georgi

    georgi Active Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #8
    this is what i have now as hotlinking protection

    RewriteEngine On
    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(.+\.)?bing\.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^http://[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/translate_c [NC]
    RewriteCond %{HTTP_REFERER} !^http://[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/search [NC]
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://mysite.com/hotlink.jpg [L]
    Code (markup):
     
    georgi, Jan 29, 2013 IP
  9. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #9
    Write. Personally, I would just deny them altogether, rather than redirecting to another image. It's all anti-hotlinking, just personal preference on how it's done. Use whichever method you prefer.
     
    ryan_uk, Jan 29, 2013 IP