How To Protect Your Images and files with htaccess

One of the most annoying problem for every blog or website owner is how to protect your hosted images and files on your website, If you usually have lots of images on your site you may noticed that some people start linking (Hotlinking) to these images and this of course will increase your bandwidth usage.

So it’s very important that you protect files and images on your site to save your bandwidth, the way to do this is by using the htaccess on your host to protect your files and images.

How to protect your images?

Open up your htaccess and modify it with the following code, You need to change yoursite.com with your domain name.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?yoursite.com(/)?.*$ [NC]
RewriteRule .(gif|GIF|jpg|JPG)$ /hotlinking/banned.gif [L,NC]

in this line /hotlinking/banned.gif you can define image with text on it for example (banned image), this image will be displayed instead of your hosted images if someone tried to link to your images outside your site.

If you have other images extensions you can add to (gif|GIF|jpg|JPG).

How to protect your Files?

The following code will completely forbid any one try to download files outside your website, and will redirect it to your website.

just change yoursite.com with your domain name, and change redirecturl to the url that you want to shoe when someone try to download yourfiles. Also add any other files extension you want to protect to (rar|RAR|ZIP|zip)

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www.)?yoursite.com/.*$ [NC]
RewriteRule .(rar|RAR|ZIP|zip)$ http://www.redirecturl.com [R,L]

Now what if you wanted to specify a cretin websites to allow them to show your images and download your files?

You can add this code if you wanted to allow some website (friendlysite) to show you images or download your files, and change friendlysite to the sites that you want to allow.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.friendlysite1.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://friendlysite1.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.friendlysite2.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://friendlysite2.com.*$ [NC]

Related posts:

  1. How to copy Text from images Using JOCR
  2. How to Copy Files To & From Your iPhone
  3. How to Password Protect DVD/CD or USB Memory
  4. How To Mount ISO Files As a Virtual CD/DVD Drive
  5. How to Password protect your Disk

Rate this topic:
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading ... Loading ...
Popularity: 253 views
This item was filled under [ Tips & Tricks ]
Tagged with: [ , , , ]
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Comment on “How To Protect Your Images and files with htaccess”

Leave a Comment