hot link protection via htaccess

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)

Rich (BB 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?
 
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;
}
 
Is up to you, did you read the documentation? Nginx "aint" a copy/paste "thing" from Internet. :)
 
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. :(
 
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]
 
Top Bottom