Forum Moderators: phranque

Message Too Old, No Replies

Allowing image search engines to hotlink images (and ban the rest)

A close to perfect anti-hotlinking .htacess

         

silverbytes

3:30 am on Mar 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi! This is best I can do it by now (and I appreciate you point me in the right direction on learning regular expressions the easy way)

I need to prevent hotlinking but allow most of image search engines. My code:


#anti-hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com\.ar(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !(google¦msn¦yahoo¦picsearch¦altavista¦ditto¦ask¦live¦aol)\.com(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://images\.google\. [NC]
RewriteCond %{HTTP_REFERER} !^http://.*\.search\.yahoo\.com [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule .*\.(gif¦jpg¦jpeg¦bmp¦png)$ - [F,NC]

Line 3 is to allow my own site

Line 4 wants to let all those image search engines hotlink (some of these starts like this: "something.yahoo.... does the ! symbol means "contains"?

Line 5 and 6 are just in case ;) I don't want to loose yahoo and google if my code in line 4 doens't work hahah

Line 7, allow cached images.

I also have a custom 403 error (403.shtml) is there any downside in using a custom instead default?

Help appreciated!

jdMorgan

3:59 am on Mar 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the beginning of a mod_rewrite pattern, "!" Means NOT.

The absence of both a start- and end-anchor, "^" and "$", in a regular-expressions pattern means that you only care if the string contains text matching the pattern, and not what position the match occurs in.

Cleaning up redundant lines characters and sub-patterns:


#anti-hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com\.ar/? [NC]
RewriteCond %{HTTP_REFERER} !(google妃sn尖ahoo如icsearch地ltavista圬itto地sk奸ive地ol)\.com/? [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule \.(gif夸pe?g在mp如ng)$ - [F,NC]

Please review the material cited in our forum charter and the posts in our library, as I seem to recall making very similar recommendations for a very similar thread today...

Thanks,
Jim

silverbytes

4:39 am on Mar 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you. 2 questions:

If domain name is: my-domain

It should be escaped? ie: my\-\domain

And what about sites like yahoo.co.uk or yahoo.es?
If I understand correctly this line:!(google妃sn尖ahoo如icsearch地ltavista圬itto地sk奸ive地ol)\.com/

will take care of

yahoo.com
yahoo.com.something

but no yahoo.co.uk

Am I right? Should I just change the ending part?

RewriteCond %{HTTP_REFERER} !(google妃sn尖ahoo如icsearch地ltavista圬itto地sk奸ive地ol)\.[NC]