Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles

Reply
 
Thread Tools
Hotlink Protection Tutorial (Apache Server - .htaccess files)
NeutralizeR
Join Date: Aug 2005
Posts: 355

 

Show Printable Version Email this Page Subscription
NeutralizeR NeutralizeR is offline 07-24-2006, 10:00 PM

What is hotlinking and bandwidth theft?

Bandwidth theft or "hotlinking" is direct linking to a web site's files (images, video, etc.). An example would be using an <img> tag to display a JPEG image you found on someone else's web page so it will appear on your own site, eBay auction listing, weblog, forum message post, etc.

Bandwidth refers to the amount of data transferred from a web site to a user's computer. When you view a web page, you are using that site's bandwidth to display the files. Since web hosts charge based on the amount of data transferred, bandwidth is an issue. If a site is over its monthly bandwidth, it's billed for the extra data or taken offline.

A simple analogy for bandwidth theft: Imagine a random stranger plugging into your electrical outlets, using your electricity without your consent, and you paying for it.

How Do I know I am hotlinking?

This is how you might display an image graphic file in the HTML on your own web page:
HTML Code:
<img src="image.jpg" height="350" width="200">
This tag tells the site to request the file image.jpg on the same server as the rest of the files on the site. If you were to hotlink an image from an outside server, the HTML might look like this:
HTML Code:
<img src="http://notmysite.com/image.jpg" height="350" width="200">
This tag tells the site to request the image.jpg from a different server other than your own. Every time the page is loaded, the outside server has to use its bandwidth to display the image. To avoid this problem, don't link to files on servers that don't belong to you. To share images and files on your own web page, upload them to your own server's directory or to a free image hosting service that allows direct linking.

Why should I stop hotlinking?

Hotlinking can have a lot of undesirable consequences. One is the so-called "switcheroo". If you've linked to an image on someone's server, what's to prevent them from changing the image you linked to? This can have humorous results. Since most sites, forums, etc. have strict policies about offensive images, it wouldn't take much for an aggravated webmaster you've been stealing bandwidth from to shut you down completely with an unwanted "switcheroo".

Displaying an image or file that doesn't belong to could be a violation of copyright, making you open to litigation. The owner of the file could utilize DMCA law to have your site shut down and your information given for use in legal proceedings.

How can I test to see if my image can be hotlinked?

Use our URL hotlink checker below to check the hotlinking protection (such as an htaccess file) on your web site. Enter the complete URL below (ex: http://mysite.com/image.jpg) to see if your image can be loaded and hotlinked by an outside server.

Click to go to the Resource & Test Page

******************************************
******************************************
******************************************
Following text may not be an ultra professional experience but i've been using these techniques for 2 years and they are working great for me.

I've been running two dedicated servers and i've full control over them (my .htaccess file lines included in my httpd.conf file). If your hosting is shared and got some restrictions to use .htaccess files on your account, you should contact your hosting company first to enable them.
Notice: .htaccess files can only be used by Apache web servers and they will not work on a windows system.

Hotlink Protection Enabled .htaccess File Tutorial

-Create a new TXT file named *'sample.htaccess' and open it with your text editor (NotePad). *Windows users won't be able to create it as '.htaccess' so you have to rename 'sample.htaccess' to '.htaccess' after it's been uploaded to your server.

-Copy and paste the following lines to 'sample.htaccess' file:
Quote:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com$ [NC]
http://www.yourdomain.com/forum/ [R,NC]
-Change RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.yourdomain.com with your domain name.
-Upload 'sample.htaccess' file to your web server's root and rename it to '.htaccess'.

Read the following text to figure out how to customize your own .htaccess file.

RewriteCond %{HTTP_REFERER} !^$ = Allow direct requests (ie. entering the url to an image in your browser). People can't publish your image files at their own web pages but they still can view them by entering their url in the browser window. These images can also be viewed (shared) by clicking on their urls in Instant Messenger windows.

[NC] = "No Case", meaning match the url regardless of being in upper or lower case letters.

[R] = Redirect

*(jpg|jpeg|gif|png|bmp|swf) = Files to block

I used to redirect blocked files to a custom .jpe image file. If you prefer this option, you should use the following .htaccess file:
Quote:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.yourdomain.com/nohotlinking.jpe [R,NC]
Upload a tiny jpg file with a text on it like "Hotlinking is not allowed!" and change it's extension to .jpe. This JPEG image is using the extension jpe instead of jpg to prevent blocking your own replacement image.

My .htaccess file:
Quote:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org$ [NC]
RewriteRule .*\.(mp3|mpeg|mpg|ram|rm|wma|wav|asx|wmv|avi|mov|z ip|rar|exe)$ http://www.msxlabs.org/forum/ [R,NC]

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.msxlabs.org/forum/ [R,NC]
This .htaccess file redirects people to my forum homepage who are trying to access the file types which are listed in the blocked files list.

First piece of lines got RewriteCond %{HTTP_REFERER} !^$ line as i don't want people to be able to access those kind of files directly. (The protection for the image files doesn't have that line)

If you don't want a redirection for (mp3|mpeg|mpg|ram|rm|wma|wav|asx|wmv|avi|mov|zip|r ar|exe), you can use replace that line with the following display your Error Code 404 page:

Quote:
RewriteRule .*\.(mp3|mpeg|mpg|ram|rm|wma|wav|asx|wmv|avi|mov|z ip|rar|exe)$ [F,NC]
Example: 404 - MsXLabs

.htaccess files effect the folder it's in and all of the subfolders.

If you want to exclude a subfolder from the hotlink protection, create another .htaccess file with the lines below and upload it to that directory:
Quote:
RewriteEngine on
RewriteRule ^.*$ -
IMPORTANT NOTE: PLEASE DELETE ANY EXTRA SPACES ADDED BY VBULLETIN. (jpg|jpeg|gif|png|bmp|swf) THERE SHOULD'T BE ANY SPACES BETWEEN THE EXTENSION TYPES.
Feel free to reply this thread if i made some mistakes

This tutorial is written by NeutralizeR @ MsXLabs Organization
Reply With Quote
  #22  
Old 10-03-2006, 05:42 AM
NeutralizeR NeutralizeR is offline
 
Join Date: Aug 2005
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Sorry, my mistake: it is post #5. I'm sleepy at the moment, hope i understood you correct:

Code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainguest1.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainguest1.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainguest2.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainguest2.com$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainguest3.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domainguest3.com$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.domain.com/nohotlinking.jpe [R,NC]
Reply With Quote
  #23  
Old 11-03-2006, 01:54 PM
Lionel Lionel is offline
 
Join Date: Dec 2001
Location: Delray Beach, Florida
Posts: 3,277
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

And how do you deal with attachment.php?

This is what they are stealing from me. htaccess does not protect that
Reply With Quote
  #24  
Old 11-03-2006, 04:55 PM
NeutralizeR NeutralizeR is offline
 
Join Date: Aug 2005
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lionel
And how do you deal with attachment.php?

This is what they are stealing from me. htaccess does not protect that
If guests can't view/download attachments, they won't be able to view/download your attachments on an external site. If you want to test it, just logout and click the link on the external site.
Reply With Quote
  #25  
Old 11-03-2006, 10:05 PM
Lionel Lionel is offline
 
Join Date: Dec 2001
Location: Delray Beach, Florida
Posts: 3,277
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Guest are allowed to see attachments. But I found a solution that works perfect for me. Attachment.php displays on my site only.
Reply With Quote
  #26  
Old 11-26-2006, 05:18 PM
HostileAdam HostileAdam is offline
 
Join Date: Jul 2005
Posts: 62
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How would i make it so that a certain file is allowed to be hotlinked, like affiliates for example how would i allow people to hotlink my link button? and then the rest of the .gifs wont be allowed to be hotlinked
Reply With Quote
  #27  
Old 11-27-2006, 01:48 AM
puertoblack2003's Avatar
puertoblack2003 puertoblack2003 is offline
 
Join Date: Aug 2005
Location: Philadelphia
Posts: 1,073
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

is there something we can use for windows server i know this method don't work that way
Reply With Quote
  #28  
Old 01-18-2007, 08:50 AM
Greek76's Avatar
Greek76 Greek76 is offline
 
Join Date: Aug 2006
Location: Planet Earth
Posts: 440
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I added this code to my existing htaccess code obviously replacing the urls with my own and now none of my images are appearing on the forum. No photopost, forum icons ect.. Do you know what might be causing this? Thanks

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org$ [NC]
RewriteRule .*\.(mp3|mpeg|mpg|ram|rm|wma|wav|asx|wmv|avi|mov|z ip|rar|exe)$ http://www.msxlabs.org/forum/ [R,NC]

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.msxlabs.org$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.msxlabs.org/forum/ [R,NC]
Reply With Quote
  #29  
Old 01-18-2007, 09:08 AM
NeutralizeR NeutralizeR is offline
 
Join Date: Aug 2005
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Use this one below:
Code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.yourdomain.com$      [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf|mp3|mpeg|mpg|ram|rm|wma|wav|asx|wmv|avi|mov|zip|rar|exe)$ http://www.yourdomain.com/forum/ [R,NC]
Change http://www.yourdomain.com/forum/ to anything you want to redirect to...

(a page, a picture with .jpe extension)
Reply With Quote
  #30  
Old 01-20-2007, 11:30 AM
aacircle aacircle is offline
 
Join Date: Jun 2005
Posts: 108
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great tutorial. However my redirect isn't working. Hotlinking is being blocked but it doesn't redirect to our home page.

I use:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.animeonline.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.animeonline.net$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.animeonline.net/ [R,NC]

You can see a test page at www.myotherdomain.com/test.htm. There is a broken image that should redirect to AnimeOnline's home page. Correct?

Thank you.
Reply With Quote
  #31  
Old 01-20-2007, 12:11 PM
NeutralizeR NeutralizeR is offline
 
Join Date: Aug 2005
Posts: 355
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi,

Image files are displayed as they are broken with hotlink protection enabled, they can't redirect to another page.

I use one line for any file type to use less server resource:
jpg|jpeg|gif|png|bmp|swf|mp3|mpeg|mpg|ram|rm|wma|w av|asx|wmv|avi|mov|zip|rar|exe

If they link a zip file pointing to your domain, it redirects to your homepage if somebody clicks on it. This method doesn't work for image files.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:55 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04538 seconds
  • Memory Usage 2,313KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (2)bbcode_code
  • (2)bbcode_html
  • (6)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (11)post_thanks_box
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete