Prevent Image leeching in Apache (complete article)

Source: Internet
Author: User
I believe many of my friends have encountered the theft of links to files on the website, especially images. The so-called leeching refers to the direct link between the other website and the files on your website, rather than placing the files on your own server. Generally, the leeching objects are mostly large files with high bandwidth consumption, film, Video, etc. in a sense, this is actually

I believe many of my friends have encountered the theft of links to files on the website, especially images. The so-called leeching refers to the direct link between the other website and the files on your website, rather than placing the files on your own server. Generally, the leeching objects are mostly large files with high bandwidth consumption, in a sense, this actually causes you to pay for your access traffic: Not only is your server bandwidth occupied without any return, but also, it usually affects the access speed of your website in a large program.

Of course, from a strict moral and legal perspective, even if the website of the other party does not steal the chain, even if the file is copied to its own server for use, you should first obtain your permission. However, in the Internet world, it is often unnecessary to break the rules. This makes many people get used to using others' content and resources in website construction. the Source website itself cannot do anything about this. In this regard, Chinese websites are in the leading position in the world. under the influence of the traditional Chinese "culture" and the Great Leap Forward of education, compared with traditional Chinese websites, Chinese websites tend to be arrogant in terms of plagiarism and leeching, the saying that "not stealing books" or even "stealing your books is worthy of you" has even become the mainstream. Last year, I met a "best man". because of the serious leeching of a website image, the server bandwidth is heavily occupied, instead, we had to adopt the most primitive anti-Leech measures-the website used IIS and did not install the Mod_rewite function extension module-that is, the way to change the image file storage directory and file name, as a result, I received a protest from a leeching website, accusing me of how immoral I am, and causing him a lot of trouble: he had to modify each webpage one by one so that the images could be correctly displayed. he taught me to keep the image url unchanged to become more professional.

Although this type of popularity is not common, for original websites, leeching may be a better choice if they are prohibited from leeching at the beginning of website construction, of course, it can also save a lot of trouble for the best man.

Object leeching prevention goals and requirements

The following describes the goals we want to achieve:

In the root directory of the website. htaccess settings: Although theoretically speaking, in Apache, you can set each directory separately. htaccess implements complex functions through permission inheritance and overwrite. However, too many. htaccess statements may increase the difficulty of management. sometimes, the modification of. htaccess may cause website problems.
By default, other websites are prohibited from leeching: for image files, when other websites use leeching, they are automatically redirected to images similar to the picture shown in the right figure, declaring copyright and promoting your website. Of course, from the perspective of reducing the server bandwidth usage, the image file cannot be too large. you can also simply reject the image file and replace it with the red "x.
Allow specific access sources: simply for image files, in fact, it is impossible to prohibit all access except your own website, such as Google. if you want to get a certain access through Google image search, users must be able to read the real image files correctly, and the RSS subscription users should be able to see the images in the feed. This requires access from bloglines and so on.
Allow files in a specific directory to be used by external websites: it is inconvenient to completely prohibit external websites. in many cases, we may also need to use some files on external websites. Of course, there must be restrictions on the files that are placed in these directories. Otherwise, the significance of anti-Leech settings will be lost.
Set. htaccess to disable image leeching

The following section describes how to set. htaccess to prevent Image leeching in the blog school:

RewriteEngine on
RewriteCond % {REQUEST_URI} ^/(allow1 | allow2)
RewriteRule ^. * $-[L]

First, set the directory where files that can be used by external websites can be "leeching". we have set two directories: allow1 and allow2. of course, if you only have one directory, you can change it:

RewriteCond % {REQUEST_URI} ^/allow1

Next, determine whether it is an image file:

RewriteCond % {REQUEST_FILENAME}. (gif | jpeg | png) $ [NC]

You can also set more file types as needed.

RewriteCond % {HTTP_REFERER }! ^ $

The above line is intended to allow access with an empty "HTTP_REFERER", that is, to 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.

RewriteCond % {HTTP_REFERER }! Blogsdiy.org [NC]
RewriteCond % {HTTP_REFERER }! Google.com [NC]
RewriteCond % {HTTP_REFERER }! Baidu.com [NC]
RewriteCond % {HTTP_REFERER }! Bloglines.com [NC]
RewriteCond % {HTTP_REFERER }! Feedburner.com [NC]

Set the HTTP source that can be accessed, including blog schools, Google, Baidu, Bloglines, and Feedburner.

RewriteRule (. *)/allow1/leech.gif [R, NC, L]

Redirect the irrelevant content to leech.gif. You can see that leech.gif is located in allow1, which allows "leech.gif". Otherwise, your warning images cannot be displayed on the website of the other party.

In this way, we have achieved our preset goal of preventing Image leeching.

Anti-Leech settings for other types of files

If your website has other files with high bandwidth consumption, such as flash files and mp3 files, which are leeched by other websites, you can also adopt the above policies, for example, for Flash files, the following settings are available:

RewriteCond % {REQUEST_URI} ^/allow1
RewriteRule ^. * $-[L] RewriteBase/
RewriteCond % {REQUEST_FILENAME}. swf $ [NC]
RewriteCond % {HTTP_REFERER }! ^ $
RewriteCond % {HTTP_REFERER }! Blogsdiy.org [NC]
(...... Other permitted access sources)
RewriteRule (. *)/allow1/leech.swf [R, NC, L]

Of course, you should first create a flash file named “leech.swf that declares copyright information ". Other settings, such as preventing mp3 files and compressed files (zip or rar) from leeching, are similar.

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.