Preventing image hotlinking - is this code correct?

Hi,

I noticed that some websites are linking to my images and I want to prevent this but at the same time I want to allow Google and other search engines to display my images on image search. I have this code in my .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^(.+\\.)?mydomain.com/.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} !^(.+\\.)?google\\. [NC,OR]
RewriteCond %{HTTP_REFERER} !^(.+\\.)?yahoo\\. [NC,OR]
RewriteCond %{HTTP_REFERER} !^(.+\\.)?bing\\. [NC]
RewriteRule \\.(gif|jpe?g|bmp|png|js|css)$ http://www.mydomain.com/stop-hotlinking.gif [R,L]

I wanted to make sure if this is the right code to use before uploading it to my website.

Thanks for any ideas.

ademmeda,

That looks good to me (except that I’d use R=301 rather than just R).

Regards,

DK

Just be aware tyhat you MAY need to always allow the ‘stop-hotlinking’ gif through the rules too - assuming its on the same host as the rules are there to protect.

Tim,

Hmmm, good thought! I’ve never seen that addressed but it’s a very valid comment!

Regards,

DK

I can dig out what I had to do on a previous site to allow this if needed - i had to forcibly allow the ‘block’ image through otherwisse it got stuck in an infinite loop.

Tim,

Not needed. All that would have to be done is to add another RewriteCond to examine the {REQUEST_URI} and NOT match against the “pirate” image.

Regards,

DK

Ah yes… I am awake today… honest :wink: