Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set image hotlink protection using .htaccess

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Use .htaccess how to set up picture hotlink protection, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The really useful htaccess is written as

RewriteEngine on

RewriteCond% {HTTP_REFERER}! ^ $[NC]

RewriteCond% {HTTP_REFERER}! jb51.net [NC]

RewriteCond% {HTTP_REFERER}! youdao.com [NC]

RewriteCond% {HTTP_REFERER}! zhuaxia.com [NC]

RewriteCond% {HTTP_REFERER}! twitter.com [NC]

RewriteCond% {HTTP_REFERER}! facebook.com [NC]

RewriteCond% {HTTP_REFERER}! xianguo.com [NC]

RewriteCond% {HTTP_REFERER}! google.cn [NC]

RewriteCond% {HTTP_REFERER}! google.com [NC]

RewriteCond% {HTTP_REFERER}! google.com.tw [NC]

RewriteCond% {HTTP_REFERER}! google.com.sg [NC]

RewriteCond% {HTTP_REFERER}! google.com.hk [NC]

RewriteCond% {HTTP_REFERER}! bloglines.com [NC]

RewriteCond% {HTTP_REFERER}! soso.com [NC]

RewriteCond% {HTTP_REFERER}! mail.qq.com [NC]

RewriteCond% {HTTP_REFERER}! cn.bing.com [NC]

RewriteCond% {HTTP_REFERER}! image.baidu.com [NC]

RewriteCond% {HTTP_REFERER}! feedburner.com [NC]

RewriteCond% {HTTP_REFERER}! feedsky.com [NC]

RewriteRule. (png | jpg) $/ no.gif [Rmaine NCMAL]

I uploaded the .htaccess file along with the no.gif file to the root directory, and notice the last line. When an image of the specified type (png or jpg) appears on a site that is not in the above list, the image is automatically replaced with no.gif.

Explain the above statement:

1. RewriteCond% {HTTP_REFERER}! ^ $[NC]

Allow empty "HTTP_REFERER" access, that is, the display of the picture file when the user is allowed to enter the image address directly in the browser address bar. In general, this is optional, but it is recommended that if you are forced to have "HTTP_REFERER" to access, it may cause some problems, such as when the user accesses through the proxy server.

2. RewriteCond% {HTTP_REFERER}! zhukun.net [NC]

Set the HTTP sources allowed to access, including our own site, Google, Baidu, Bloglines, Feedburner, feedsky, and so on.

3. RewriteRule. (jpg | png) $/ no.gif [Rmaine NCMAL]

Define the images that are replaced when they are stolen, and let all the web pages of stolen jpg, png and other images display the no.gif files in the root directory. Note: there must be a "/" here, and the smaller the no.gif, the better, and the smaller the better, the more traffic saving.

4. Explain R, NC and L

R means to turn.

NC means case-insensitive.

L indicates that this turn is over, and the subsequent turn is not affected by the previous judgment statement.

Special note: it is not recommended to prohibit gif | jpg | png image files at the same time. For example, you have banned gif, but replaced the hotlink images with no.gif, which will definitely cause problems. If you want to ban all three types at the same time, you can use the following methods:

RewriteRule. (gif | png | jpg) $http://farm3.static.flickr.com/2743/4462030670_dc36df0e3d_o.png [RMagneNCmael]

That is, replace the presentation with pictures from other servers (no hotlink protection has been set).

This method has been successfully tested on the free space of Godaddy. Once again, please indicate the source of the original article of Qingfeng Xiaotang.

The following is a supplement to other netizens, with the direct local exception of a directory

RewriteEngine on

RewriteCond% {REQUEST_URI} ^ / images

RewriteRule ^. * $- [L]

RewriteCond% {REQUEST_FILENAME}\. (jpg | gif | bmp | zip | rar | mp3 | txt | png | wma | wmv) $[NC]

RewriteCond {HTTP_REFERER}! ^ $

RewriteCond% {HTTP_REFERER}! ^ $[NC]

RewriteCond% {HTTP_REFERER}! www.laoyao.me [NC]

RewriteCond% {HTTP_REFERER}! laoyao.me [NC]

RewriteCond% {HTTP_REFERER}! www.zhuaxia.com [NC]

RewriteCond% {HTTP_REFERER}! www.google.com [NC]

RewriteCond% {HTTP_REFERER}! google.com [NC]

RewriteCond% {HTTP_REFERER}! www.google.com.hk [NC]

RewriteCond% {HTTP_REFERER}! google.com.hk [NC]

RewriteCond% {HTTP_REFERER}! www.google.cn [NC]

RewriteCond% {HTTP_REFERER}! google.cn [NC]

RewriteCond% {HTTP_REFERER}! www.baidu.com [NC]

RewriteCond% {HTTP_REFERER}! www.baidu.cn [NC]

RewriteCond% {HTTP_REFERER}! baidu.com [NC]

RewriteCond% {HTTP_REFERER}! baidu.cn [NC]

RewriteCond% {HTTP_REFERER}! bloglines.com [NC]

RewriteCond% {HTTP_REFERER}! www.ask.com [NC]

RewriteCond% {HTTP_REFERER}! www.163.com [NC]

RewriteCond% {HTTP_REFERER}! www.yahoo.com [NC]

RewriteCond% {HTTP_REFERER}! www.sogou.com [NC]

RewriteCond% {HTTP_REFERER}! www.soso.com [NC]

RewriteCond% {HTTP_REFERER}! www.sina.com.cn [NC]

RewriteCond% {HTTP_REFERER}! www.msn.com [NC]

RewriteCond% {HTTP_REFERER}! www.feedsky.com [NC]

RewriteCond% {HTTP_REFERER}! www.feedburner.com [NC]

RewriteRule (. *) / images/error.gif [Rmaine NC Magi L]

Code description: the second line of the code, which means to open the permissions of the images directory of the website, that is to say, the pictures under the images folder do not have hotlink protection, why? Because our hotlink warning picture is placed in this directory, if you do not open this directory permission, then when others steal the chain, even this hotlink warning picture can not be displayed, only a red fork can be displayed, which is not the effect we want. The fourth behavior of the code requires the suffix name of the hotlink protection file, which can be modified as needed. The code behind means which sites can not be hotlink protection, are some search engines and so on, as well as the URL of their own station. The warning picture displayed when the last behavior of the code steals a chain. You can also store this picture on other websites, such as some image storage space, so that you can save more traffic on this site.

Supplementary method 3:

.htaccess is written as follows, download the attachment to this article, including the PSD source file. Thoughtful, in order not to do pictures, as long as open to change the URL can be used, of course, you can also design your own! Foreign virtual host traffic is too small, picture anti-theft link must be done.

RewriteEngine on

RewriteCond% {HTTP_REFERER}! ^ $[NC]

RewriteCond% {HTTP_REFERER}! yziw.net [NC]

RewriteCond% {HTTP_REFERER}! youdao.com [NC]

RewriteCond% {HTTP_REFERER}! zhuaxia.com [NC]

RewriteCond% {HTTP_REFERER}! twitter.com [NC]

RewriteCond% {HTTP_REFERER}! facebook.com [NC]

RewriteCond% {HTTP_REFERER}! xianguo.com [NC]

RewriteCond% {HTTP_REFERER}! google.cn [NC]

RewriteCond% {HTTP_REFERER}! google.com [NC]

RewriteCond% {HTTP_REFERER}! google.com.tw [NC]

RewriteCond% {HTTP_REFERER}! google.com.sg [NC]

RewriteCond% {HTTP_REFERER}! google.com.hk [NC]

RewriteCond% {HTTP_REFERER}! bloglines.com [NC]

RewriteCond% {HTTP_REFERER}! soso.com [NC]

RewriteCond% {HTTP_REFERER}! mail.qq.com [NC]

RewriteCond% {HTTP_REFERER}! cn.bing.com [NC]

RewriteCond% {HTTP_REFERER}! image.baidu.com [NC]

RewriteCond% {HTTP_REFERER}! feedburner.com [NC]

RewriteCond% {HTTP_REFERER}! feedsky.com [NC]

RewriteRule. (gif | png | jpg) $http://www.yziw.net/images/linkeldiy.png [RMagneNCmael]

Note: the last line of the replacement picture should be changed to your own

Prohibit downloading of image link .htaccess files

Continue to add.

First explain the picture hotlink protection and redirection:

What is the use of Q picture hotlink protection?

A prevent other websites from stealing your pictures and wasting your precious traffic.

What is the use of Q picture turning?

An if your website is mainly based on pictures, and one day you find that you will run out of traffic before the end of the month, you can use image redirection and redirect the image download request to other spaces (such as trial host) without modifying the web page. temporary transition.

Let's start with the explanation. For example, if all your images are in the img directory, put a file called .htaccess in that directory, as follows:

RewriteEngine on

RewriteCond% {HTTP_REFERER}! ^ $[NC]

RewriteCond% {HTTP_REFERER}! chong4.com.cn [NC]

RewriteCond% {HTTP_REFERER}! zhuaxia.com [NC]

RewriteCond% {HTTP_REFERER}! google.com [NC]

RewriteCond% {HTTP_REFERER}! baidu.com [NC]

RewriteCond% {HTTP_REFERER}! bloglines.com [NC]

RewriteRule. (jpg | gif | png | bmp | swf | jpeg) / image/replace.gif [RMagneNCmael]

RewriteRule ^ (. *) $http:\ /\ / image.chong4.com.cn\ / image\ / $1 [L]

Give a general explanation:

RewriteCond% {HTTP_REFERER}! ^ $[NC]

RewriteCond% {HTTP_REFERER}! chong4.com.cn [NC]

RewriteCond% {HTTP_REFERER}! zhuaxia.com [NC]

RewriteCond% {HTTP_REFERER}! google.com [NC]

RewriteCond% {HTTP_REFERER}! baidu.com [NC]

RewriteCond% {HTTP_REFERER}! bloglines.com [NC]

This part is to determine whether to steal links. If the above conditions are true (that is, the request to access the image is neither directly entered into the URL, nor from chong4.com.cn, nor from zhuaxia.com, nor from google.com, nor from baidu.com, nor from bloglines.com), perform the following redirection:

RewriteRule. (jpg | gif | png | bmp | swf | jpeg) / image/replace.gif [RMagneNCmael]

It means that all the images displayed on the web pages of jpg, gif, png, bmp, swf and jpeg files in the img directory will be replaced with the replace.gif images in the image directory. Be careful not to put the replacement image in the img directory where hotlink protection is set.

If it is determined that the image request is not hacked according to the above rules, perform the following redirection:

RewriteRule ^ (. *) $http:\ /\ / image.chong4.com.cn\ / image\ / $1 [L]

It means that all requests under the img directory will be directed to the target server. For example, an image whose original url is http://www.chong4.com.cn/img/girl.jpg will now be transferred to http://image.chong4.com.cn/image/girl.jpg. Of course, you have to copy all the files in the img directory of the original server to the image directory of the temporary server before you can really use it. The effect is to save all the traffic occupied by the original server picture download and let the temporary server bear it.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report