Hot linking or bandwidth stealing is a common problem. It happens when people link to files and images on a different server, display them on their website and the bandwidth is at the other person's expense.
Code:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?askapache.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http://www.askapache.com/hotlink.gif [R,L]
1. Activate Protection
Stop people from hotlinking to your image files.
Code:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?askapache.com/.*$ [NC]
RewriteRule \.(bmp|tif|gif|jpg|jpeg|jpe|png)$ - [F]
Replace your_domain.com with your domain name.
This starts by turning on the rewrite engine. Next, it checks the referer (where the request comes from).
If it did not come from your domain name and it is a request for a .gif or .jpg file, it will output an error message. Otherwise it will display the image as usual.
The rewrite engine is very powerful and can be used for many other purposes. This tutorial is for beginners.
2. Other Uses
This .htaccess trick can be used to protect other file types, such as .mp3, .ogg, .mpg and other files.
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?your_domain.com/.*$ [NC]
RewriteRule \.(mp3|ogg|mpg)$ - [F]
The only changes in this .htaccess file are on the last line. Enter the file extensions in the brackets, do not include the dot (.). Separated them with a pipe (|).