Welcome to WebmasterWorld Guest from 54.146.180.94

Forum Moderators: Ocean10000 & incrediBILL & phranque

hotlink code

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

10+ Year Member



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)

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



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)

5+ Year Member



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)

10+ Year Member



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)

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



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)

5+ Year Member



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)

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



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

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month