How to use .htaccess to prevent hotlinking website images_PHP tutorial

WBOY
Release: 2016-07-13 10:30:47
Original
718 people have browsed it

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]
Copy after login

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.


RewriteCond %{HTTP_REFERER} !^$ [NC]

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]
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]

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.

Articles you may be interested in

  • .htaccess How to set up anti-hotlinking for images in a directory
  • Use .htaccess to deny access to a website by a certain IP
  • Use Apache’s htaccess to prevent images from being linked to theft
  • Use .htaccess ban list directories
  • 10 practical .htaccess code snippets
  • Set through .htaccess Website default homepage
  • How to modify the website’s default homepage through .htaccess?
  • php uses curl to implement multi-threaded classes, php curl multi-threaded download pictures

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764200.htmlTechArticleAnti-hotlinking website pictures, as the name suggests, is to prevent external websites from stealing the pictures of our website. Its function is to prevent others from stealing pictures from our website. The pictures of our website are posted on the blog and space website...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template