homepage Welcome to WebmasterWorld Guest from 54.196.197.153
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
hotlink code
mihomes




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

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?

 

lucy24




msg:4307564
 10:44 pm on May 3, 2011 (gmt 0)

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.

jabz




msg:4307769
 12:22 pm on May 4, 2011 (gmt 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).

mihomes




msg:4308409
 6:36 pm on May 5, 2011 (gmt 0)

Thanks... appreciate the responses... I will give this one a try and see how it goes.

g1smd




msg:4308420
 7:08 pm on May 5, 2011 (gmt 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]
jabz




msg:4308438
 7:58 pm on May 5, 2011 (gmt 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.

g1smd




msg:4308451
 8:10 pm on May 5, 2011 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved