This site utilizes JavaScript to function correctly. Looks like it's disabled on your browser. Please enable it for your best experience.

For instructions on enabling JavaScript, click here

Skip to main content

Just Host Web Hosting Help

Hotlink Protection Using mod_rewrite

Summary

Due to either ignorance or a "I'll do what I want because I want to" attitude, there are plenty of people that will place image tags on their pages that pull images from your server. This linking can place a greater load on your Just Host server/account without having visitors actually seeing your website.
Note: You can enable Hotlink Protection in the cPanel, Hotlink Protection Setup.

How does mod_rewrite work?

The Apache Server's Mod Rewrite Engine can examine the name of the document requesting a file of a particular type. You can then define logic that basically does the following:

If the URL of the page requesting the image file is from an allowed domain, display the image. Otherwise, return a broken image.

The logic, or rules, are then placed in the directory(s) that contain your image files.

Setup Leech Protection with mod_rewrite

In order for it to work, the browser that requested the page must return the URL of the page, or what is called the HTTP_REFERRER. There is also a performance penalty on the server due to the extra overhead for testing the file requests.

This method should be used when off site linking has become an issue of concern to you. A little bit of tolerance or maybe a gentle e-mail to the other site's webmaster may also be an acceptable solution. I have actually made a few friends this way!

  1. Make certain that your Apache Server was compiled with mod_rewrite. By default, the basic installation of Apache does not include it. Do not attempt this if mod_rewrite is not installed, or your site may stop functioning! Check with your system administrator. If you are the system administrator, check the Apache INSTALL file for instruction regarding the option --enable-module=rewrite


  2. Get organized! Try to get all of your images into directories that do not contain your HTML files. Each directory containing the images should have an empty index.html file to prevent people from looking at your directory listing.

  3. Create or edit a .htaccess in one of the directories containing your images. I suggest doing one directory first so you can test your rules, and quickly comment out the lines or rename the file if it causes server configuration errors. The .htaccess file should contain the following lines.

    RewriteEngine on
                		RewriteCond %{HTTP_REFERER} !^$
                		RewriteCond %{HTTP_REFERER} !^http://domain.com/.*$     [NC]
                		RewriteCond %{HTTP_REFERER} !^http://www.domain.com/.*$ [NC]
                		RewriteRule .*.gif$    -                            [L]
                


    RewriteCond %{HTTP_REFERER} !^$
                		RewriteCond %{HTTP_REFERER} !^http://domain.com/.*$     [NC]
                		RewriteCond %{HTTP_REFERER} !^http://www.domain.com/.*$ [NC]
                		RewriteRule .*.jpg$      -                           [L]
                

    Note: When cutting and pasting, be sure that each RewriteCond is on one line. Line wrapping in the page display could introduce broken lines.

  4. Test! Create a page on another server and insert in image tag pointing to an image in the protected directory. If you get a broken image icon- you did it! The requests will still appear in your logs, but your bandwidth will be protected.
Knowledgebase Article 209,719 views bookmark tags: htaccess rewrite


Was this resource helpful?

Did this resolve your issue?


Please add any other comments or suggestions about this content:





Recommended Help Content

I need to protect the /images folder to not show images when people type in the correct URL like www.mydomain.com/images/ to protect from prying eyes.
Knowledgebase Article 174,491 views tags: hotlink htaccess protection

How do I perform a URL redirect/rewrite using the .htaccess file?
Knowledgebase Article 1,937,997 views tags: htaccess redirect rewrite url

How do I setup Hotlink Protection?
Knowledgebase Article 205,874 views tags: link protection

Related Help Content

Do you support mod_rewrite for apache?
Knowledgebase Article 271,057 views tags: apache mod rewrite

Explains how to increase your bandwidth.
Knowledgebase Article 87,064 views tags: bandwidth

Where are the reports on disk space, file count, and bandwidth usage?
Knowledgebase Article 299,180 views tags: bandwidth diskspace usage

The redirection of www.yourdomain.com/default.html or index.html to http://www.yourdomain.com/index.php?act=whatever redirects it to http://www.yourdomain.com/index.php%3fact=whatever In other words,
Knowledgebase Article 1,037,057 views tags: htaccess messing mod php redirect redirects rewrite variable variables

What is the .htaccess file and where can I find more information?
Knowledgebase Article 2,084,059 views tags: htaccess tips tricks

Why is my site not the first result on Google or an other search engine?
Knowledgebase Article 271,772 views tags: bing engine google search seo yahoo

Explains how to edit the .htaccess
Knowledgebase Article 371,176 views tags: htaccess

How to run CGI scripts for Addon domains.
Knowledgebase Article 234,076 views tags: cgi htaccess

** Google ad credits are only available to customers in the United States, Canada, and the United Kingdom at this time.

¹ VAT (Value Added Tax) is not included in our advertised price and will be charged separately and itemized on invoices and billing information. Standard VAT rates based on EU Member State regulations may apply. Learn more.