Welcome to WebmasterWorld Guest from 23.22.220.37

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

hotlink code

     
8:48 pm on May 3, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:May 21, 2004
posts: 374
votes: 4


Noticed I am having issues with hotlinked images and came across this bit of code :


RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/hotlink.gif [L]


My understanding of the above is that any image format listed which is hotlinked outside of mysite.com will be redirected to the /images/hotlink.gif file under my public_html folder. Great, but how would I allow sites such as google, yahoo, bing, etc who have image searches to still show my images in their results?

Second, is there a better solution of the above code or anything else that is recommended?
10:44 pm on May 3, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12702
votes: 244


Quick-and-simple answer: robots arrive with null referrers, so you've already let them in. (My logs confirm this.)

The code version you've posted-- hm, looks like you and I swiped from the same source ;)-- points visitors to an alternative image you've created. Mine's intentionally ugly so users will say "Ouch!" and take it down quickly. There are other approaches, especially if bandwidth is an issue. But just replacing your gorgeous multi-megabyte jpg with a little 150x150 16-color png already saves you a bundle.

Oh, and the !^$ should probably say !^-?$ to allow for servers (or other in-transit software) that changes empty strings to -. Someone somewhere in these Forums said so.
12:22 pm on May 4, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 4, 2006
posts: 53
votes: 0


From what I have learned on this forum, the redirect target should always contain the protocol and domain name.

Which leaves us with this:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^-?$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://mysite.com/images/hotlink.gif [L]


In the RewriteRule, mysite.com can also be external (I guess).
6:36 pm on May 5, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:May 21, 2004
posts: 374
votes: 4


Thanks... appreciate the responses... I will give this one a try and see how it goes.
7:08 pm on May 5, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


The leading .* at the beginning of the pattern is redundant.

A redirect with domain name and using only the
[L]
flag generates a 302 redirect.

I would use
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F]
7:58 pm on May 5, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 4, 2006
posts: 53
votes: 0


OK, so we either have...

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^-?$
RewriteRule \.(jpe?g|gif|bmp|png)$ [mysite.com...] [NC,R,L]

...in case we want to display another image (make sure not to generate a loop).

OR...

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^-?$
RewriteRule \.(jpe?g|gif|bmp|png)$ - [F]

...to thow a 403 Forbidden.
8:10 pm on May 5, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


On its own, the [R] flag generates a 302 redirect.