Portal Home > Knowledgebase > How To's and more > How to prevent HotLinking of your Images


How to prevent HotLinking of your Images




OK your probably wondering what hot linking is. Hot linking is theft of your images and your bandwidth. Someone who is hot linking has not only stole your images to place on there site, they have actually linked straight to them from there pages and are making your server serve them to there visitors. 

So how do we prevent hot linking. If your on a UNIX sever its pretty simple. We will be using .htaccess again so if you've followed one of my previous tutorials using .htaccess its recommended you use that file again otherwise you'll lose what ever you did in that tutorial.

OK what we need:
A text editor. Preferably one that can save in UNIX format. One that I prefer TextPad. You can get a trial version here. Or just open your simple Notepad. 
A third party ftp program that supports the Chmod command. One that I prefer is SmartFTP. Overall SmartFTP is free, you can download it here.

Step 1
Open TextPad and start a new document if it didn't for you. Then go to file chose "Save As", then "All Files" and "UNIX". Name it .htaccess If you have previously made a .htaccess file with one of my previous tutorials, please open that file now. 

Note: If you followed the "Restrict access to Certain Pages" tutorial, you'll need to still create a new .htaccess file because we will be placing this one in a different location.

Step 2
Decide if your just going to block the request or if your feeling nasty and would like to server a different image that reflects your feelings about what the person(s) is doing. 

Step 3
For those that are just going to block the request copy this code and change mydomain.com to match your domain name, place this .htaccess in your document root, and your done.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ - [F]

For those that are wanting a little revenge use the following code and change the domain name, image name and path to match. In order for this option to work you need to have the .htaccess file and replacement image in your document root. If you have any images in your root directory and someone links to them it will cause an infinite loop. So if you can help it do not place images that are likely to be linked to in your root directory.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
RewriteRule .(gif|jpg)$ http://www.mydomain.com/assets/nasty.gif [R,L]

Step 4
Uploading your files.

Open your ftp client, connect to the server, and navigate to the document root. Then find your .htaccess file on your local hard drive, and upload it to your document root. Make sure to upload in ASCII and not BINARY. Finally, Chmod it to 644 or RW-R--R--. 

Now if you decided to replace the hot linked image with another, find your local copy of the image and upload to the path you specified in the .htaccess file. (Should be the same place as your .htaccess file.)

Congratulations!! You just stopped some thief from stealing your images and your costly bandwidth, unless you decided to serve them another image. 

Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read