Tutorial on how to implement image anti-leech using. htaccess on Apache server

Source: Internet
Author: User
Tags password protection

Http://www.phpweblog.net/phpers/archive/2008/01/02/2632.html

This section describes how to use mod_rewrite of Apache to configure anti-image (or download) leeching.

I recently found a good article about file leeching for your reference,

An issue that users who build their own blogs often face is the server traffic, and site file leeching is the most important part. The so-called leeching refers to the files directly linked to our website by other websites. Generally, the leeching objects are mostly large-volume files, videos, and videos that consume a lot of bandwidth. The main consequences include increasing the server traffic burden and affecting the access speed of our website.

If you are using an Apache server, you can easily set the. htaccess file to Prevent Users on other sites from leeching files on our site.

The. htaccess file in Apache

. The htaccess file is an important configuration file in Apache. Its format is plain text. It provides a method for changing the configuration of directories, you can place a file containing one or more commands in a specific document directory to act on this directory and all its subdirectories.

Pass. the htaccess file can implement many simple functions that are cumbersome or even unable to be implemented in IIS, such as password protection, prohibiting the display of directory lists, blocking/allowing specific IP addresses, and implementing 301 redirection of the website.

As mentioned above ,. the htaccess file will affect its directory and its sub-directories. Therefore, if the content we want to protect (This section uses anti-image leeching as an example, that is, images) is located in multiple directories on the website, you can consider placing the image in the root directory. If the image has a separate subdirectory such as "/images /", you only need to put it in this directory (of course, you can also put it in the root directory ).

Note that if you upload the created. htaccess file to the server through FTP, the transmission mode should be ASCII, not binary. After uploading to the server, you should change its properties to 644 or "RW-R-R-" Through chmod, so that the server can be used and cannot be modified through the browser, of course ,. the Readable attribute of htaccess also has a certain risk: attackers can use it to find the object you want to protect or the location of the authentication file-the solution is to authenticate the file. put htpasswd outside the root directory of the website, so that it cannot be found through the network.

Use. htaccess to disable leeching

Pass. htaccess is a simple method to prevent unauthorized links to non-HTML files such as images, compressed files, or videos on websites. By adding a few commands to the file, we can protect our valuable bandwidth. For example, the settings on this site are as follows:

Rewriteengine on

 

Rewritecond % {http_referer }! ^ $ [Nc]

 

Rewritecond % {http_referer }! Webeta.cn [Nc]

 

Rewritecond % {http_referer }! Ikuaizi.com [Nc]

 

Rewritecond % {http_referer }! Zhuaxia.com [Nc]

 

Rewritecond % {http_referer }! Xianguo.com [Nc]

 

Rewritecond % {http_referer }! Google.com [Nc]

 

Rewritecond % {http_referer }! Bloglines.com [Nc]

 

Rewritecond % {http_referer }! Feedburner.com [Nc]

 

Rewritecond % {http_referer }! Feedsky.com [Nc]

 

Rewriterule. * \. (GIF | JPG) $ http://webeta.cn/no.png [R, NC, l]

 

Briefly explain the preceding statements:

1. rewritecond % {http_referer }! ^ $ [Nc]

Allow access with "http_referer" Blank, that is, allow users to directly enter the image address in the browser address bar to display the image file. In general, this is optional. However, we recommend that you set this parameter. If you force a request to have "http_referer" to access the service, this may cause some problems, for example, when the user accesses the service through the proxy server.

2. rewritecond % {http_referer }! Webeta.cn [Nc]

Set the HTTP source that can be accessed, including the website itself, Google, Baidu, Bloglines, and feedburner.

3. rewriterule. * \. (GIF | JPG | PNG) $ http://webeta.cn/no.png [R, NC, l]

Defines the images to be replaced when a chain is stolen, so that all webpages that steal JPG, GIF, and PNG files will display the No. PNG files in the root directory. Note: Do not place the displayed image in the anti-leech directory. The smaller the size of the image file, the better. You can use the following statement instead of replacing the image:

Rewriterule. * \. (GIF | JPG | PNG) $-[F]

 

4. Describe the R, NC, and l

R means turning.

NC is case insensitive.

L indicates that the current redirection ends, and subsequent redirection is not affected by the previous judgment statement.

5. file types to prevent leeching

In the preceding example, GIF, JPG, and PNG are used. You can change or add other file types, such as RAR and mov, as needed. Different file extensions are separated by "|.

In this way, we can basically prevent leeching and minimize the fearless consumption of server traffic. Of course, if you don't care about this traffic, so you don't have to consider the above settings!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.