Forum Moderators: phranque

Message Too Old, No Replies

Hotlinking rule issue

self domain being blocked by my rules!

         

waveking

2:34 pm on May 27, 2006 (gmt 0)

10+ Year Member



Hi,
I need to prevent people from hotlinking my image files, so i have put in the following code in my htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER}!^http://(.+\.)?example\.com/ [NC]
RewriteRule .*\.(jpg夸peg夙if如ng在mp)$ /images/stolen.png [R,NC,L]

This seems to be working now, and if i open my own site from www.example.com, the site shows the images, BUT if i prefix 'http://', i.e. if i type http://www.example.com, then my own domain is unable to show the images... please help me correct my rules

<snip>

Thanks.

[edited by: jdMorgan at 3:23 pm (utc) on May 27, 2006]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

3:28 pm on May 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> "if I type http://..."

If you type in a URL, there will be no HTTP_REFERER, so your rule will redirect rhe image request.

It is generally necessary to *allow* blank referrers, unless you want your site to appear to be broken when a user comes from behind a corporate or ISP caching proxy:


RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/images/stolen\.png
RewriteRule \.(jpe?g夙if如ng在mp)$ http://www.example.com/images/stolen.png [R=302,NC,L]

It is also necessary to by-pass the rule if the image request is for "stolen.png" -- Otherwise, you'll end up in a redirection loop.

Jim

waveking

3:53 pm on May 27, 2006 (gmt 0)

10+ Year Member



Thanks for the prompt response jdMorgan.

I was not hitting a redirect loop becuase I had placed the stolen.jpg in a folder, other then the one having the htaccess. I tried putting in the blank referral rule, but that didnt help.

But, on some other research, i added the following line of rule:
RewriteCond %{HTTP_REFERER}!^http://(www\.)?example\.com [NC]

So, now my full ruleset reads:

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?example\.com/ [NC]
RewriteCond %{REQUEST_URI} !^/images/stolen\.png
RewriteRule .*\.(jpg夸peg夙if如ng在mp)$ /images/stolen\.png [R=302,NC,L]

seems to be working for me now, though I don't understand why :(

[edited by: jdMorgan at 4:28 pm (utc) on May 27, 2006]
[edit reason] No uRLS, please. See Terms of Service. [/edit]