Tutorials Home
Search Tutorials
Submit a tutorial
Restrict image theft with htaccess
Restrict image theft with htaccessHotlinking is the term to describe another website posting your images on their website by directly taking the URL of your website.

This tutorial will help you understand how to stop websites stealing your images for use o...

View Tutorial
Details
  Rating 0.00.00.00.00.0  
Vote You need to log in to vote!  
Submitted By: cybanworld  
Comments: No Comments - Be the first!  
Facebook StumbleUpon Delicious Digg Reddit Blinklist BlueDot Magnolia NetVouz Blogmarks Simpy Diigo Yahoo MyWeb
Read the tutorial Hotlinking is the term to describe another website posting your images on their website by directly taking the URL of your website.

This tutorial will help you understand how to stop websites stealing your images for use on their websites. Hotlinking is not only "theft", but also increases the bandwidth usage on your server, as the image is coming directly from your server.


Many free web hosts prevent hotlinking as default, however, some dont, and if you want to stop this from happening, its time to learn about htaccess.


What is htaccess?
Htaccess is a directory level configuration file. Its used most commonly with apache servers, and allows you, to override certain sections of the servers global configuration file.

So what does it actually do? Well, a number of things. It can be used to restrict access to pages or directories using a password protection system (See this tutorial), it can redirect users who get those horrible default server errors (such as 404 errors) to a much more friendlier error document, and so on.

This particular htaccess file should be saved in the root of your websites directory tree, IE, if you were to be able to view it in a browser, you would go to: http://www.yoursite.com/.htaccess

The Htaccess file is saved in a special way. Its not called htaccess.html, its not called htaccess.php etc, its called .htaccess
The period at the beginning implies to a *nix system that it is a hidden file. It cannot be directly accessed by the browser.


Creating the file
Before you go on, you should ensure that a htaccess file isnt already stored on your server. Overwriting an existing htaccess file may cause your website to stop working, or not function as expected.

So, if your server has a htaccess file, download it and open it in a text-editing program. If not, open up a blank file (notepad, dreamweaver etc).

Save this file as .htaccess
Note: Make sure this file is called '.htaccess' only. If you save it in a text editing program, it may append a file extension to it (such as .htaccess.txt or .htaccess.php).


The Code
This next step will tell you what to write to prevent hotlinking.
Lets look at the entire code to start with, then analyze it.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yoursite.com [NC]
RewriteRule \.(jpe?g|jpg|bmp|gif|png)$ http://subdomain.yoursite.com/no_hotlinking.gif [NC]


So lets look at what this actually does.
RewriteCond %{HTTP_REFERER} !^$
Some visitors who come to your website have a firewall (as most web-savy users have). Firewalls can remove the referer header information. This line will allow users without a referer header still access your images on your server. Its generally advised you keep this line included!

RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yoursite.com [NC]
This line tells the server what websites are allowed to access your images. The string http(s)?://(www\.)?yoursite.com tells the server that your website is allowed to display images. Obviously, change yoursite.com to whatever your website is.
The string [NC] tells the server that the domain name is Not Case-sensitive.

RewriteRule \.(jpe?g|jpg|bmp|gif|png)$ http://subdomain.yoursite.com/no_hotlinking.gif [NC]
Between the () symbols, you can specify what type of files you with to restrict access to. Seperate each file type with a | symbol.
The next URL that follows allows you to specify an image you wish to replace the image being stolen. This image should be on a seperate server, otherwise it wont be able to be displayed due to it being restricted as well!

Save this portion of code to your .htaccess file and upload it to your root directory. Your images should now stop being displayed on external websites!

Comments
No comments have been posted!
Before you can comment, please Register or login.