.htaccess & hotlinking question

Discussion in 'Apache' started by procrastinator, Mar 19, 2007.

  1. #1
    I know by ".htaccess" one can prevent hotlinking of images ,

    via this

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

    However ,my question is can i prevent hotlinking of images based on size concideration ? for eg not hotlink if images are above X kb .

    Bascially , i am running a simple image hosting script and if i blindly use the above code , even my thumbnails will be blocked from viewing on forums/websites , i do not want that, i just want the larger images to not be hotlinked.

    My host itself has a hotlink protection thing in CP , via which i can block hotlinking, however even the thumbnails dont show :(
     
    procrastinator, Mar 19, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure that's possible but even if it is, you don't want your server to have to lookup a filesize every time an image is requested. A much easier way to achieve the same effect is to be consistent with image naming and apply the rewrite depending on that. In other words, if you create thumbnails and name them thumb_imagename.jpg, you can rewrite only requests for thumb_*.

    RewriteRule thumb_.*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpe [L]
     
    rodney88, Mar 20, 2007 IP
  3. procrastinator

    procrastinator Peon

    Messages:
    1,718
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    0
    #3
    oh yeah thats a good idea ..i didnt know about that.. but the code didnt work..needless to say .. im not really adept at all this

    currently the htaccess file has ... all hotlinking is blocked

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http://mysite.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://mysite.com$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://mysite.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://mysite.com$ [NC]
    RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://mysite.com [R,NC]

    can u tell me what should i edit here?

    also .. my tnail's have the same filename as the images, (not any tnail_xyz.jpg) if there is someway i can allow a certain folder to be hotlinked
    images/thumbs/
    is where my thumbnails lie , if access is allowed to this folder to be hotlinked it would do the trick .
     
    procrastinator, Mar 20, 2007 IP
  4. procrastinator

    procrastinator Peon

    Messages:
    1,718
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yes i did it ....

    [​IMG]

    [​IMG]

    i added this

    RewriteCond %{REQUEST_URI} ^/images/thumbs/
    RewriteRule ^.*$ - [L]

    ... any suggetion on this people?
     
    procrastinator, Mar 20, 2007 IP