Home > Backend Development > PHP Tutorial > How to prevent image hotlinking under Apache server

How to prevent image hotlinking under Apache server

WBOY
Release: 2016-07-25 08:45:03
Original
809 people have browsed it

First explain the image hotlink protection and redirection:

What is the use of image hotlink protection?

 Prevent other websites from stealing your images and wasting your precious traffic.

 What is the use of image redirection?

 If your website is mainly based on images, and one day you find that the traffic is almost used up before the end of the month, then you can use image redirection to redirect image download requests to other spaces (such as trial hosts) without modifying the web page. ), temporary transition.

Let’s start with the explanation. For example, if your pictures are in the img directory, then put a file named .htaccess in that directory with the following content:

  1. RewriteEngine on
  2. !zhuaxia.com [NC]
  3. RewriteCond %{HTTP_REFERER} !google.com [NC]
  4.  RewriteCond %{HTTP_REFERER} !baidu.com [NC]
  5.  RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
  6.  RewriteRule .(jpg|gif|png |bmp|swf|jpeg) /image/replace.gif [R,NC,L]
  7.  RewriteRule ^(.*)$ http://image.simcole.cn/image/$1 [L]
  8. Copy code
A rough explanation:

  RewriteCond %{HTTP_REFERER} !^$ [NC]
      RewriteCond %{HTTP_REFERER} !simcole.cn [NC]
  1.   RewriteCond %{HTTP_REFERER} !zhuaxia.com [NC]
  2.  RewriteCond %{HTTP_REFERER } !google.com [NC]
  3.  RewriteCond %{HTTP_REFERER} !baidu.com [NC]
  4.  RewriteCond %{HTTP_REFERER} !bloglines.com [NC]
  5. Copy code
 This part is to determine whether the link is hotlinked. If the above conditions are true (that is, the request to access the image is neither directly entered in the URL, nor from simcole.cn, nor from zhuaxia.com, nor from google.com, nor from If it comes from baidu.com or not from bloglines.com), perform the following redirection:  

RewriteRule .(jpg|gif|png|bmp|swf|jpeg) /image/replace.gif [R,NC,L]
  1. Copy code
 It means that all web pages that hotlink jpg, gif, png, bmp, swf, and jpeg files in the img directory will replace the displayed images with the replace.gif image in the image directory. Note that the replaced image should not be placed in the img directory where anti-hotlinking is set. If it is determined according to the above rules that the image request is not a hotlink, the following redirection will be executed:

 RewriteRule ^(.*)$ http://image.simcole.cn/image/$1 [L]
  1. Copy code
 It means that all requests under the img directory will be redirected to the target server. For example, the original URL of a picture is http://img.it-home.org/data/attachment/forum/2016pic1/girl.jpg. Now it is It will go to http://img.it-home.org/data/attachment/forum/2016pic1/girl.jpg. Of course, you must first copy all the files in the img directory of the original server to the image directory of the temporary server before the redirection will be truly available. The effect is to save all the traffic occupied by downloading images from the original server and let the temporary server bear it.


Hotlinking, Apache
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