homepage Welcome to WebmasterWorld Guest from 54.166.84.82
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

10+ Year Member



 
Msg#: 4307521 posted 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

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



 
Msg#: 4307521 posted 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

5+ Year Member



 
Msg#: 4307521 posted 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

10+ Year Member



 
Msg#: 4307521 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4307521 posted 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

5+ Year Member



 
Msg#: 4307521 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4307521 posted 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