HTML5, CSS3, and JavaScript Fourth Edition

(Ben Green) #1

CHAPTER 19. FIRST WEBSITE 216


Leech Protection


Many websites block outside linking to media on their website, or sometimes
even to webpages on their website. Such deep linking is calledleeching(as
in the blood-sucking leech). Using.htaccessrewrite rules, we can make it
so the picture is only viewable from our own website.


One reason to prevent leeching is the desire to display advertising near their
images. Deep linking retrieves just the image and not the advertising.


Another reason to prevent leeching is the desire to reduce bandwidth usage.
Say I have a webpage, pretty small, but it has links to large images that are
stored elsewhere. My webhost only charges me for the small webpage, and
not for the large images. The webhost of the large images gets to pay for
the bandwidth each time someone looks at my webpage.


On the other hand, some websites request you to link to their images. It
gives them a way to measure how often the images are retrieved. It goes in
their log files. They want that.


We will show you how to prevent leeching.


In this example, we check the referrer. If it is doncolton.com, we allow the
request to continue. Otherwise, we apply the rewrite rule and replace the
URL with noleech.jpg. Presumably noleech.jpg would be an image telling
the user that the requested image is not available.


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://.*doncolton.com/.$ [NC]
RewriteRule .
.$ http://doncolton.com/noleech.jpg [R,NC]


This stops all requests for jpg and png files that are part of the don-
colton.com domain, unless the referrer is also in the doncolton.com domain.

Free download pdf