Tilt - 12 track CD of Scottish Techno

Preventing offsite linking to audio files

When running a music site there is nothing more annoying than 3rd party web sites hotlinking your mp3s - particularly where dj mixes are concerned. This uses valuable bandwidth without the downloader ever having visited you site, read your forum or registered as a user. Fortunately it is quite simple to prevent with the use of a little file called .htaccess (note the preceeding dot).

Requirements

You will need to be running an apache web server with the mod_rewrite module loaded. If you are not sure whether or not this is the case save the following line to a text file called phpinfo.php:-

< ? phpinfo(); ?>

Upload this file to your web server and then load it into your browser eg http://www.yoursite.com/phpinfo.php. Fairly close to the top of the page you should find a section called either apache or apache2handler. At the bottom of this section is the loaded modules - mod_rewrite should be one of the values to the right.

Excerpt from phpinfo showing mod_rewrite

The .htaccess file

The next stage is to create the .htaccess file. Once again create a new text file and insert the following:-


RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yoursite.com/ [NC]
RewriteRule [^/]+.(mp3|MP3)$ http://www.yoursite.com [R,L]

Replace yoursite.com with your domain name in lines 3 & 4. Line 3 details the domain that is allowed to download the files - if you wish more than one domain to have access to them simply duplicate this line. Line 4 redirects people to your homepage if they click on a link to one of your mp3s from another site. Save this file as .htaccess and upload it to your mp3 folder on your website. And that’s it!

Use of this is of course not just limited to mp3 files - it can be used to prevent leeching of images for instance by changing the file type in the RewriteRule on line 4.

Leave a Reply