Modify the. htaccess file to disable image leeching and resource leeching.

Source: Internet
Author: User
Tags cpanel file manager password protection

The leeching of images and other resources will greatly waste our traffic. Even if the VM does not limit the traffic, it will also waste our bandwidth. Today, I occasionally see an article on the Internet to prevent leeching. After some practice, it is very effective. Now I will share my practices for your reference only.

1. The. htaccess file in Apache

. The htaccess file, also known as the "distributed configuration file", is a very important configuration file in Apache. It is in plain text format and provides a way to change the configuration of directories, place a file in a specific document directory to act on the directory and Its subdirectories.
Pass. the htaccess file can implement many simple functions that are cumbersome or even unfeasible in IIS, such as password protection, prohibiting column directories, blocking or allowing specific IP addresses, and implementing 301 redirection of URLs.
To protect images and other resources from leeching, we need to put the file in all the parent root directories or each directory to protect the resources. WordPress can be directly stored in the WP-Contents/uploads directory.
This file property should be set to 644 or RW-R-R -"

2. Add commands to. htaccess to prevent leeching.

In general, set
<Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritebase/
Rewritecond % {http_referer }! ^ $
Rewritecond % {http_referer }! ^ Http: // (www /.)? Ucooper.com (/)?. * $ [Nc]
Rewriterule. */. (GIF | JPG | JPEG | BMP | PNG) $-[F]
</Ifmodule>
Note:

(1) Change ucooper.com to your domain name on the fourth line.
(2) the fifth act is the file type to prevent leeching. You can add it as needed and separate it with "| ".
(3) We generally want to remind others of the words "do not leeching" in their leeching locations, so we can change the fifth line to this and use our images to remind them.
Rewriterule. */. (GIF | JPG | JPEG | BMP | PNG) $ http://www.ucooper.com/alert.gif
[R, NC, 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.
(4) In Wordpress, the file may already exist in the directory. You can use the cpanel File Manager to edit the file. It is best not to overwrite the original command, for example, change it to the following. If this file does not exist, you can copy one from other places to the corresponding directory.

Content of the. htaccess file added under the WP-content/uploads directory

<Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond % {http_referer }! ^ $
Rewritecond % {http_referer }! ^ Http: // (www /.)? Ucooper.com (/)?. * $ [Nc]
Rewriterule. */. (GIF | JPG | PNG | zip | PDF | DOC) $ http://www.ucooper.com/alert.gif
[R, NC, l]
</Ifmodule>

The. htaccess In the WordPress root directory does not need to be modified.

# Begin WordPress
<Ifmodule mod_rewrite.c>
Rewriteengine on
Rewritebase/
Rewriterule ^ index/. php $-[l]
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
Rewriterule./index. php [l]
</Ifmodule>
# End WordPress

(5) The website provides some functions to automatically generate the file. You can give it a try.

Http://www.wangqu.org/htaccess/#a_Pic

3. Test it.

Add images on our site to other blogs to test the effect. My display effect is as follows:

4. To further reduce traffic, we recommend that you do not place images on your website, but you can also place images on a graph bed such as flicker.

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.