1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

hot link protection via htaccess

Discussion in 'Server Configuration and Hosting' started by Adam Howard, Aug 29, 2012.

  1. Adam Howard

    Adam Howard Well-Known Member

    I've hit a wall....

    The Goal

    Block site X and show them image X
    Blocked site Y and show them image Y
    Block everyone else and show them image Z

    Have a safe list / white list, so that I can still hot link here on XenForo and a few other select sites.

    What Jake came up with (but doesn't seem to work)

    Code:
    # NO HOTLINK FOR SITE X
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?sitex\.com [NC]
    RewriteCond %{REQUEST_URI} !nohotlinkingx\.jpg$ [NC]
    RewriteRule \.(jpg|gif|jpeg|png|bmp|pdf|zip|txt|svg)$ http://www.yoursite.com/nohotlinkingx.jpg? [NC,L]
     
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?sitex\.com [NC]
    RewriteRule ^attachments/.+$ http://www.yoursite.com/nohotlinkingx.jpg? [NC,L]
     
    # NO HOTLINK FOR SITE Y
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?sitey\.com [NC]
    RewriteCond %{REQUEST_URI} !nohotlinkingy\.jpg$ [NC]
    RewriteRule \.(jpg|gif|jpeg|png|bmp|pdf|zip|txt|svg)$ http://www.yoursite.com/nohotlinkingy.jpg? [NC,L]
     
    RewriteCond %{HTTP_REFERER} ^http://(www\.)?sitey\.com [NC]
    RewriteRule ^attachments/.+$ http://www.yoursite.com/nohotlinkingy.jpg? [NC,L]
     
    # NO HOTLINK FOR ALL SITES
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yoursite\.com [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowedsite\.com [NC]
    RewriteCond %{REQUEST_URI} !nohotlinking\.jpg$ [NC]
    RewriteRule \.(jpg|gif|jpeg|png|bmp|pdf|zip|txt|svg)$ http://www.yoursite.com/nohotlinking.jpg? [NC,L]
     
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?yoursite\.com [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowedsite\.com [NC]
    RewriteRule ^attachments/.+$ http://www.yoursite.com/nohotlinking.jpg? [NC,L]
    
    I know someone here must have done this before.

    Can anyone here tell me what is wrong with this code?
     
    Brandon Sheley likes this.
  2. Floren

    Floren Well-Known Member

    In Nginx, you can replace the army of conditions listed above with 3 lines of code:
    Code:
    valid_referers none blocked server_names;
    if ($invalid_referer) {
    	return 404;
    }
     
    maszd likes this.
  3. Mouth

    Mouth Well-Known Member

    Where does this nginx code go?
     
  4. Floren

    Floren Well-Known Member

    Is up to you, did you read the documentation? Nginx "aint" a copy/paste "thing" from Internet. :)
     
  5. Adam Howard

    Adam Howard Well-Known Member

    Well.... Still not working..... Given up on using more than 1 photo and so I tried this...

    PHP:
    # hotlink protection allowing for multiple domains
    RewriteCond %{HTTP_REFERER}  .
    RewriteCond %{HTTP_REFERER}  !^http://([^.]+\.)?sociallyuncensored\. [NC]
    RewriteCond %{HTTP_REFERER}  !search\?q=cache [NC]
    RewriteCond %{HTTP_REFERER}  !google\. [NC]
    RewriteCond %{HTTP_REFERER}  !bing\. [NC]
    RewriteCond %{HTTP_REFERER}  !yahoo\. [NC]
    RewriteCond %{REQUEST_URI}   !^/hotlink\.jpg$ [NC]
    RewriteRule \.(gif|jpg|png)$ http://www.sociallyuncensored.eu/hotlink.jpg [R,NC,L]
    No luck. :(
     
  6. Adam Howard

    Adam Howard Well-Known Member

    SOLUTION

    This one work :D

    PHP:
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?sociallyuncensored.eu [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?sociallyuncensored.com [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?bing.com [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yahoo.com [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?xenforo.com [NC]
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?twitter.com [NC]
    RewriteRule \.(jpg|jpeg|png|gif|svg|css|js|pdf|zip)$ http://www.sociallyuncensored.eu/hotlink.jpg [NC,R,L]
     
    ArnyVee and Jake Bunce like this.

Share This Page