Need .htaccess file help for image hosting site , hotlinking issue

Discussion in 'Apache' started by procrastinator, May 3, 2007.

  1. #1
    my site is http://www.imagepundit.com

    basically i want to allow hotlinking , but i want to block certain domains to hotlink. can this be done?
     
    procrastinator, May 3, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    As you're already forcing all image requests through a script, it may be easier to add this in via PHP to the top of gallery.php rather than through .htaccess.
    $from = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];
    if ( $from ) {
        # Banlist
        $toBan = array();
        $toBan[] = 'domaintoblock.com';
        $toBan[] = 'another2block.com';
        $toBan[] = 'a3rdbannedone.com';
        # Extract domain from referer
        preg_match('@^https?://(www\.)?([^/]+)/@i',$from,$tmp);
        $domain = empty($tmp[2]) ? false : $tmp[2];
        unset($tmp);
        # Check domain against banlist
        if ( $domain && in_array($domain,$toBan) ) {
             header('Location: http://www.imagepundit.com/nohotlink.jpg');
             exit;
        }
    }
    PHP:
    But if you want to use mod_rewrite, something like this should do (in imagepundit.com/.htaccess):
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_REFERER} domaintoblock.com [OR]
    RewriteCond %{HTTP_REFERER} another2block.com [OR]
    RewriteCond %{HTTP_REFERER} a3rdbannedone.com
    RewriteRule gallery\.php nohotlink.jpg [L]
    Code (markup):
    Obviously duplicate the line that checks referrer and ensure all RewriteConds except the last have the [OR] flag.

    Both untested but should be along those lines.
     
    rodney88, May 3, 2007 IP
  3. procrastinator

    procrastinator Peon

    Messages:
    1,718
    Likes Received:
    56
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i just want the thing in the htaccess file , currently the file looks garbage

    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/images/thumbs/
    RewriteRule ^.*$ - [L]
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^http://www.imagepundit.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.imagepundit.com$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.imagepundit.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.imagepundit.com$ [NC]
    RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ www.imagepundit.com [R,NC]
    
    Code (markup):
    i want a simple thing to allow hotlinking to everyone , but not to some websites , but i would like them to still see the thumbnail images hence
    RewriteCond %{REQUEST_URI} ^/images/thumbs/

    can someone clean it up for me :p , will give a footer link for a month on there if intrested
     
    procrastinator, May 3, 2007 IP