HowTo – Prevent Hotlinking To Your Image Files With htaccess

Filed under: Site Management

apache_logo_125pxNot every site owner wants to restrict access to the images on their site but sometimes you have a reason to do so.

You may have a site that has a number of copyright images such as a news or gossip site but for whatever reason you choose you can at least reduce the reuse of your image by others simply linking to them.

Many sites will already have a .htaccess file if you don’t then you will need to create one.

The .htaccess file is used for many reasons to give instruction to your webserver on how to process files.

Whenever the webserver accesses your account directory it will look for one of these files. If it is in the top directory then it will apply the commands to all sub-directories unless only specific directories are named in the .htaccess file setup.

Because the windows file system is picky about files with no prefix ie no prefix.something you will most likely need to create the file with one then rename it once it is on your server.

Start by making a new NotePad file named x.htaccess or open your current one for editing

Add the following

lines with # are comment lines
the first line requests modrewrite to be ON you may not need that on your specific server.

RewriteEngine On
#Replace ? with your website url
RewriteCond %{HTTP_REFERER} !^http://(.+.)? [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/dontcopy.jpg with the url of your copyright image
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/dontcopy.jpg [L]

This code will tell the server that only is allowed to use your images.

If someone else links to your images like

they will be sent your copyright.jpg image

You should make an image that says something like visit for stuff.

If you only want to stop a few specific sites use the following

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+.)? [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+.)? [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+.)? [NC]
#Replace /images/dontcopy.jpg with the url of your copyright image
RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/dontcopy.jpg [L]

Add more sites by using the OR command and on the final site do not add OR.

So this Rule says stop Myspace OR blogspot OR livejournal and end site list.

Make sure you upload the file as Ascii and then rename it to .htaccess

Final Note

Doing this can cause trouble if you want to allow other specific sites access to your images.  And it will not stop people from manually downloading your images and using them.

If you have cPanel, Plesk or another control panel for your website there may be a feature to generate this file through a web interface.