6.5. Restricting Images from Being Used Off-Site
Problem
Other sites are linking to images on your system, stealing bandwidth from you and incidentally making it appear as though the images belong to them. You want to ensure that all access to your images is from documents that are on your server.
Solution
Add the following lines to the .htaccess file in the directory where the
images are, or to the appropriate <Directory> container in the httpd.conf file. Replace the
myserver.com with your domain name:
<FilesMatch "\.(jpg|jpeg|gif|png)$">
SetEnvIfNoCase Referer "^http://([^/]*\.)?myserver.com/" local_referrer=1
Order Allow,Deny
Allow from env=local_referrer
</FilesMatch>In fact, by using the following recipe, you can even go one step further, and return a different image to users accessing your images via an off-site reference:
SetEnvIfNoCase Referer "^http://([^/]*\.)?myserver.com/" local_referrer=1
RewriteCond "%{ENV:local_referer}" "!=1"
RewriteRule ".*" "/Stolen-100x100.png" [L]Discussion
The first solution will cause all requests for image files to be
refused with a 403
Forbidden status unless the link leading to the request was
in one of your own documents. This means that anyone linking to your
images from a different Web site system will get the error instead of
the image, because the referer does not match the approved server
name.
Note that this technique can cause problems for requests that do
not include a Referer request header field, such as people who visit your site through ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access