Home >Backend Development >PHP Tutorial >.htaccess How to set up anti-hotlinking for images in a directory_PHP Tutorial
I won’t go into details about the significance of anti-hotlink pictures on websites! For details, please refer to the article on this site: How to use .htaccess to prevent hotlinking website images. This article mainly shares how to use .htaccess to prevent hotlinking only for pictures in a certain directory of the website.
When we used .htaccess to prevent hotlinking website images in the past, we placed the file that replaced the hotlinked image under the root directory of the website. However, the directory planning of the website rarely placed the image file in the root of the website. directory. This leads to a problem. If the file is not stored in the root directory and is set according to the previous setting method, the anti-hotlink replacement image we set will also be protected against hotlinking. The final result of this is that when others hotlink our pictures, the anti-hotlinking rules of our website will fall into an infinite loop and eventually display a cross.
So what should we do about this problem? At this time, the best way is to adjust the rules to prevent hotlinking for a certain directory on the website. Generally, the pictures uploaded by the website will be stored in a fixed folder. Now that we understand this problem, let’s use an example to illustrate how to set up anti-hotlinking rules for only a certain directory.
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/change.gif [L]
There is no explanation for the above sentences. If you don’t understand, you can refer to: Use .htaccess to prevent hotlinking website pictures for explanation. The last sentence of this code is the highlight. What this means is that the anti-hotlink setting is only for all gif jpg png pictures in the uploadfiles directory in the root directory. All hotlinked pictures in this directory will display the change.gif file in the images directory in the root directory.