Hotlink protection: tried three examples without success

I’m scratching my head over an htaccess hotlink protection conundrum. I’ve tried code snippets from three websites (see examples below) and each of them fails when I test it. Are all three variations wrong? If not, can you provide some pointers that might help me to work out what I’m doing wrong.


RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\\.)?example.com/.*$ [NC]
RewriteRule \\.(gif|jpg|png|js|pdf|vcf|eot|svg|ttf|woff)$ - [NC,F] 
--------------
RewriteCond %{HTTP_REFERER} !^http://(.+\\.)?example\\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\\.(jpe?g|gif|bmp|png)$ http://i.imgur.com/qX4w7.gif [L]
--------------
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \\.(gif|jpe?g?|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !/hotlink\\-(01|02).gif$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\\.)?example\\. [NC]
RewriteRule \\.(gif|jpe?g?|png)$ - [F,NC,L]

I replaced “example” with my domain in each case. Mod rewrite is on.

Mat,

If you’ve confirmed mod_rewrite is enabled, have you confirmed that it’s working?

I’ve assumed that hotlink.gif will not be hotlinked so the {HTTP_REFERER} will be available (to avoid the loop).

Regards,

DK

Um, right. Don’t really understand your feedback inside the quote, or your last sentence, so I can’t answer that. But I can confirm with reasonable confidence that mod rewrite must be working because other functions that use it seem to be OK.

In case it’s relevant, I found the source of the 2nd snippet: http://altlab.com/htaccess_tutorial.html. The 3rd snippet comes from somewhere on perishablepress.com. The 1st, I’ve no idea. Long forgotten!

M3,

The problem MAY be with using all three code snippets at once (presumably, you’re smart enough not to have done that but others may not see it that way and try it).

From the tutorial linked in my signature, I’d recommend ONE of the sample codes for you to use as a test:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\\.)?example\\.com/.*$ [NC]
RewriteRule \\.(gif|jpg)$ http://www.example.com/hotlinked.gif [R=301,L]

Obviously, replace example.com with your own domain name and add “hotlinked.gif” in your DocumentRoot - that’s just any image that you will recognize as being redirected from this code.

Then, from a new window or tab, attempt to link to an existing image BY TYPING THE LINK DIRECTLY (i.e., no {HTTP_REFERER}), i.e., http://www.example.com/images/logo.gif and see whether you get redirected to the hotlinked.gif image or not. If not, you have a problem with your mod_rewrite (no RewriteEngine on statement, not enabled, .htaccess not enabled, something other than the code).

Regards,

DK

Sorry, but it’s still not working. I feel like I should give up on the idea (I tend to let these little problems take up too much time!).

Thanks.