Anti-hotlinking website pictures, as the name suggests, is to prevent external websites from stealing pictures from our website. Its function is to prevent others from posting pictures from our website in other blogs and space websites, causing visitors to not enter us. website, but it still consumes the traffic of our website space. Isn't this very unpleasant? Okay, let’s take a look at how to use .htaccess to prevent others from hotlinking images on our website!
Using .htaccess to prevent hotlinking of images is actually very simple. As long as you understand the .htaccess rules, it only takes one sentence. Let’s look at a complete example below:
RewriteEngine On RewriteBase / RewriteCond %{HTTP_REFERER} !^$ [NC] RewriteCond %{HTTP_REFERER} !phpernote.com [NC] RewriteCond %{HTTP_REFERER} !google.com [NC] RewriteCond %{HTTP_REFERER} !baidu.com [NC] RewriteCond %{HTTP_REFERER} !sogou.com [NC] RewriteCond %{HTTP_REFERER} !soso.com [NC] RewriteCond %{HTTP_REFERER} !youdao.com [NC] RewriteCond %{HTTP_REFERER} !yahoo.cn [NC] RewriteRule ^uploadfiles/(.*)\.(gif|jpg|png)$ http://www.phpernote.com/images/loading.gif [L]
The following is a detailed explanation of the above code:
RewriteEngine On
URL rewriting engine switch, if set to off, any rewriting rule definition will not be applied. Another benefit of this switch is that if you want to temporarily remove the rewriting rules, change it to off and then try again. Just start Apache. There is no need to comment out the rewrite rules below. Note that this statement should be written on the first line of the file as much as possible.
RewriteBase /
The effect of this statement is the rewritten part in the rewriterule definition below (here is the file name index.html). If there is no / in front, it is a relative directory. Generally, it is set directly to /, which means the root directory.
Allow access through empty "HTTP_REFERER", that is, the image can be displayed when the user directly enters the image address in the browser address bar. Generally speaking, this is optional, but it is recommended to do the above settings, otherwise if it is forced to have "HTTP_REFERER" to access, it may cause certain problems, such as when users access through a proxy server. RewriteCond %{HTTP_REFERER} !phpernote.com [NC] What is set here is the HTTP source that is allowed to be accessed, including our own website, google.com, baidu.com, sogou.com, soso.com, youdao.com, yahoo.cn. This is also considered to allow search engines to crawl our website images normally. RewriteRule .*.(gif|jpg|png|bmp)$ http://www.phpernote.com/change.gif [R,NC,L] OK, here are the rules for setting up anti-hotlink protection. The setting here is to make all web pages that hotlink jpg, gif, png, bmp and other image files of this website display the file http://www.phpernote.com/change.gif instead. Special attention should be paid to: Do not place the replaced image in a directory with anti-hotlinking settings or include it in a directory with anti-hotlinking settings, and the smaller the file size of the image, the better. Of course, you can also not set a replacement image and use the following rules. The image of the hotlinked website will display a blank RewriteRule .*.(gif|jpg|png|bmp)$ – [F] In addition, if you don’t understand the meaning of the NC F L characters in the square brackets after the above rules, please refer to this site: .htaccess syntax parameter description Okay, after the above settings, all websites that hotlink the image files of your website will give up the hotlinking because of this unfriendly display, which to a certain extent reduces the space of your server. The necessary traffic is something worth doing.
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !google.com [NC]
RewriteCond %{HTTP_REFERER} !baidu.com [NC]
RewriteCond %{HTTP_REFERER} !sogou.com [NC]
RewriteCond %{HTTP_REFERER} !soso.com [NC]
RewriteCond %{HTTP_REFERER} !youdao.com [NC]
RewriteCond %{HTTP_REFERER} !yahoo.cn [NC]