How To Prevent Image Hotlinking
One of the most annoying things for webmasters is to find someone else using their template and their images, Especially when they hotlink to these images. Hotlinking is when your images on your server are being displayed on another site. Usually hotlinking is not too much of a problem, But if the image is large, Your own work or using a lot of bandwidth you will want to look into preventing hotlinking.
In this blog entry i will explain how to prevent hotlinking of images with a .htaccess file.
First i will post the code you need to drop into your .htaccess file and then i will explain what each line does and how it prevents hotlinking.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain(/)?.*$ [NC]
RewriteRule .*\.(gif|jpe?g|png|bmp)$ [F,NC]
The first line in this code instructs the server to turn the mod_rewrite engine on.
The second line is checking to see if the referrer string is blank.
The third line instructs the server which domains are allowed to hotlink to your images. If you want to allow more than one domain to use your images simply copy the line and paste it under it and replace the domain with the second domain allowed to hotlink your images, You can do this as many times as you want.
The fourth line tells the server what types of files you want to protect from hotlinking.
This code sample will protect all files with the extention gif, jpeg, jpg, png and bmp.
Another option for preventing hotlinking is to display an image of your choice, no matter what image is linked to.
If you are preventing hotlinking to save bandwidth you wont want to use this option because an image will still be displayed on their site, But it wont be the image they think they are hotlinking to.
Use the following code to prevent hotlinking and display an image of your choice.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain(/)?.*$ [NC]
RewriteRule \.(gif|jpe?g|png|bmp)$ /images/HOTLINKING.gif [L,NC]
This code is almost identical to the previous code, But instead of just preventing hotlinking it will display the image located on your server at /images/HOTLINKING.gif
In the examples you have to replace domain with your own domain name, Don't include your tld (.net .com .org) though, Just the domain.
Labels: hotlinking, htaccess