mod_rewrite help for hotlink protection with broken hotlink URLs

Discussion in 'Apache' started by hans, Oct 10, 2007.

  1. #1
    since long i have ten thousands/m hot linked image requests from myspace, hi5, etc.
    normal hotlinked images are successfully replaced by a replacement banner.

    however I also have large numbers of hotlink attempts where the remote site uses a broken URL - most likely a copy/paste error by hotlinker OR URL-size limit by remote host.

    such hotlink attempts with broken URLs always lead to a server error (404)
    I would like my banner still be shown - to avoid apache error as well as to make hotlinker aware of his error.

    here an example of how such a broken partial URL from a hotlinker may look:
    /wallpapers/widescreen_wallpapers/beautiful_roses/widesc

    hence part of subsolder/filename PLUS file extension are missing

    is there a way to rewrite such broken file requests WITHOUT damage to real full file requests by regular surfers coming from such referrer URLs ??
    in the same path as the hotlink request there also are regular HTML files that still should be served correctly !!
     
    hans, Oct 10, 2007 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #2
    ErrorDocument 404 /whatever.html

    with what ever you want to show up being at that file.
     
    Nintendo, Oct 13, 2007 IP
    hans likes this.
  3. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #3
    thanks nintendo
    it works like a charm :) - just like my mod_rewrite for full uRLs ...
    i was thinking complex mod_rewrite, yet your solution seems the easy perfect one
     
    hans, Oct 13, 2007 IP
  4. Ladadadada

    Ladadadada Peon

    Messages:
    382
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Of course, that will mean that every 404 on your site will cause that image to be shown... and if it's not an image, then the hotlinkers will just get a broken image, not your banner.

    You could try with something like this:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{HTTP_REFERER} !www.yoursite.com
    RewriteCond %{HTTP_REFERER} !^$
    RewriteRule ^.*$ /images/banner.png [L]
    Code (markup):
    What it all means is if what they requested is not a file (-f), a directory (-d) or a link (-l) and the referrer is not your site or empty, then send them your banner instead of the normal 404 page.

    You could add extra RewriteCond directives, such as:

    RewriteCond %{REQUEST_URI} /images
    Code (markup):
    Which would make all of the above only apply if the file they requested was in the images directory.
     
    Ladadadada, Oct 14, 2007 IP
  5. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #5
    the hotlinking problem occurs only in a particular subfolder
    hence of course i have placed that a.m. code ONLY in the htaccess of that subfolder

    all the rest of the site is hotlink protected by regular mod_rewrite with banner replacement.

    as already published above - all works perfectly now with nintendo's solution.

    however I keep your solution on file for possible later usage where needed.
     
    hans, Oct 14, 2007 IP