
05-19-2007, 12:23 AM
|
 | bujhchi!!! Developer
Gender: | | Last Online: 07-06-2008 10:53 PM Join Date: Dec 2005 Location: /dev/null/ | |
Webmaster's Tutorial: Prevent Hotlinking for Apache Web Server "Hotlinking" is a form of bandwidth theft. Basically, when creating a web page it is easy to link to a file (such as an image or a video) that exists on a remote (completely separate) site. Each time the web page is accessed, the file is retrieved from the remote web site.
Consider this example: You have created a personal gallery of cherished images. Someone else likes one of your images, so they post a "hotlink" to that image on a popular forum that they visit. Now that image is plucked from your web server every single time that page of the forum is viewed. It can quickly add up to thousands of requests, eating into your precious bandwidth allocation. To make matters worse, the image is so popular that it begins to appear in several other forums. How can hotlinking be prevented?
There are several ways you can prevent hotlinking, but the best involve adding lines to your .htaccess file. If you do not already have an .htaccess file, you can create one in a text editor - note the strange filename .htaccess. In the code below, your domain is assumed to be www.example.com. You will need to change the code to reflect your own domain name. Please note: this technique will only work on the Apache web server. Replacing Images Deny All Remote Servers:
The following code will cause the remote server to display no_hotlink.jpg instead of the requested image: Code: RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http:// [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|png)$ images/no_hotlink.jpg [R,L]
Where, NC = No Case, R = Redirect & L = Last. Allow Hotlinking from Only a Specified Directory:
The following code will cause the remote server to display 'no_hotlink.jpg' instead of the requested image, unless the image has been requested from a specified directory (dir): Code: RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/dir/ [NC]
RewriteCond %{HTTP_REFERER} ^http:// [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpeg|gif|png)$ images/no_hotlink.jpg [R,L]
Allow Hotlinking to All But Specified Domains:
The following code will cause the remote server to display no_hotlink.jpg instead of the requested image, but only when the image has been requested by badsite.net or badsite.com: Code: RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.net/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.com/ [NC]
RewriteRule \.(jpe?g|gif|png)$ images/no_hotlink.jpg [R,L]
Preventing Bandwidth Theft
Instead of replacing the image, these methods will simply deny the remote domain access to the images altogether, thus preventing any bandwidth theft. Blocking All Domains:
The following code will return a 403 Forbidden error instead of the requested image: Code: RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http:// [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|png)$ - [F]
Blocking Specific Domains:
The following code will return a 403 Forbidden error instead of the requested image, but only when the image has been requested by badsite.net or badsite.com: Code: RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.net/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.com/ [NC]
RewriteRule \.(jpe?g|gif|png)$ - [F]
Golpo is using the following code for prevent hotlinking . Code: RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www.golpo.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.golpo.net$ [NC]
RewriteRule .*\.(mp3|mpeg|mpg|ram|rm|wma|wav|asx|wmv|avi|mov|zip|rar|exe)$ http://www.golpo.net/forum/ [R,NC]
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.golpo.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.golpo.net$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|swf)$ http://www.golpo.net/forum/ [R,NC]
Collected Gaaner Ami Tumi Hariye Jabo |