Use Apache htaccess to prevent image leeching

Source: Internet
Author: User

If you do not like the pictures and documents that others steal on their web pages, you can use the ". htaccess" command. 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. Here are two solutions to leeching:

Method 1:

The required command is as follows:

RewriteEngine onRewriteCond %{ HTTP_REFERER } !^$RewriteCond %{ HTTP_REFERER } !^http://(www.)?simue.com /.*$ [NC]RewriteRule .(gif &line;jpg)$ - [F]

If you think it is difficult to open a skylight for others' pages, you can use an image instead:

RewriteEngine onRewriteCond % {HTTP_REFERER }! ^ $ RewriteCond % {HTTP_REFERER }! ^ Http: // (www .)? Simue.com/.#$ [NC] RewriteRule. (gif & line; jpg) $ http://www.simue.com/replace image file name [R, L]

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.

Method 2:

RewriteEngine onRewriteCond %{HTTP_REFERER} !^$ [NC]RewriteCond %{HTTP_REFERER} !simue.com [NC]RewriteCond %{HTTP_REFERER} !baidu.com [NC]RewriteCond %{HTTP_REFERER} !google.com [NC]RewriteRule .*\.(gif|jpg)$ http://www.simue.com /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 }! Simue.com [NC]

Set the HTTP source that can be accessed, including our website, Google, and Baidu.

3. RewriteRule. * \. (gif | jpg | png) $ http://www.simue.com/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 swf, as needed. Different file extensions are separated by "|.

Articles you may be interested in
  • How to use. htaccess anti-leech website Images
  • . Htaccess: how to set images in a directory of anti-leech
  • Disable the list directory using. htaccess
  • Php uses header () to download files. The downloaded files prompt that they are damaged and cannot be opened.
  • . Htaccess pseudo-static instance sharing
  • Usage of several keywords such as $ this, static, final, const, and self in php
  • Set the default homepage of a website through. htaccess
  • How php generates image thumbnails

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.