Logo Your Web Hosting Solution
Line
.htaccess Guide
Introduction
Directory Listing
Error Messages
Password Popup
Redirect Pages
Protect Images
Block Users
.htaccess Tools
Tutorials Index
Home Page
.htaccess Tutorial - Protect Images

Others can link to images on your site to use them on their own web site, forums, ebay and elsewhere.

This is of no benefit to you and can increase the bandwidth usage for your hosting account quite a bit.

1. Activate Protection

Add the below lines to your .htaccess file to stop people from hotlinking to your .gif and .jpg files.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?your_domain.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [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 (|).

Top Of Page 
Line
Copyright© 1996 - 2017 Clockwatchers, Inc.