Prevent Hotlinking images
Ever saw those pictures telling you that hotlinking is not allowed? Some websites seems to run some code that prevents you from seeing the pictures that are located on their servers. You can only see the pictures when surfing on their site. You thought you're the clever one and you might have taken the picture's URL from their source and pasted it on your own website, resulting in another photograph than you intended. Now, did they do that? The URL you used, makes perfect sense, but still there is another one appearing.
What you are trying to do, is stealing their bandwidth and they don't like that. So before offering the picture to you, they check the referrer. If it matches their site, the correct picture is offered, if not they present you with something else or nothing at all.
Now you might have a picture gallery yourself and you realize that people are posting pictures, that are located in your gallery, all over the internet. Well, you too can prevent that. Here's how:
All you'll need, is the mod_rewrite Apache module to do that. The module is built in by default, however some ISP's might have taken it out. Contact your ISP administrator if it's not available.
The trick to the puzzle is no more than an .htaccess file. Should you have access to your Apache config file, you can also put the code there. Have look at its content:
[cgeshi_apache]RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC] RewriteRule \.(gif|jpg|png)$ - [F][/cgeshi_apache]Let's explain. First we enable the Rewrite Engine and reformat the referrer string. Next we check the referrer string against a regular expression, representing our domainname (Be sure to replace <ins>example.com</ins> with your own domainname). If it's not us, then we deny access to anything with the gif, jpg or png extension. The image on the hotlinking website will then show up as not existing.
Seems we're almost there. Instead of denying access, we could also reformat the string and have it point to another picture. Look at the following:
[cgeshi_apache]RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC] RewriteRule \.(gif|jpg|png)$ http://www.othersite.com/nasty.gif [R,L][/cgeshi_apache]Of course the nasty.gif needs to be located elsewhere or that one too will be blocked by our rule.
Should you want to use your pictures on other websites, just add a new ReWriteCond and they should become available on that website as well.
More information on how mod_rewrite is to be used, can be found here:http://httpd.apache.org/docs/mod/mod_rewrite.html.
















