如何实现.htaccess热链接保护

时间:2013-04-16 18:04:33

标签: php .htaccess

我正在尝试对我的图片实施一些保护,我的代码出了什么问题?

// If referral is from google but NOT from "http://www.google.com/blank.html", redirect home
RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|Baiduspider) [NC]  //If user agent is NOT bot
RewriteCond %{HTTP_REFERER} !^$                                       //Allow blank referral
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]      //if referral is from google
RewriteCond %{HTTP_REFERER} ^http://www.google.com/blank.html$        //if referral is NOT from that url
RewriteRule  http://www.mydomain.com/ [R,L]                           //redirect home


// If referral is from my domain and accessing images, do nothing
RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|Baiduspider) [NC] //If user agent isn't bot
RewriteCond %{HTTP_REFERER} !^$                                      //Allow blank referral
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?mydomain.com [NC]   //if referral is from my domain
RewriteCond %{REQUEST_URI} !(^|&)images(&|$)                         //if URL contains string "images"
RewriteRule ^.*$ - [NC,L]                                            // DO nothing 


// If referral is NOT from my domain and accessing images, show watermarked image
RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|Baiduspider) [NC] //If user agent isn't bot
RewriteCond %{HTTP_REFERER} !^$                                      //Allow blank referral
RewriteCond %{HTTP_REFERER} mydomain.com                             //if referral is NOT from my domain
RewriteCond %{REQUEST_URI} !(^|&)images(&|$)                         //if URL contains string "images"
RewriteRule ^images/(.*)$ http://www.mydomain.com/cache/$1 [NC,R,L]  //redirect to watermarked image

我正在尝试创建this answer的第2步,但我遇到了“等于”和“不等于”的问题,因为我来自php和!运算符用于不等于。

帮助某人?

1 个答案:

答案 0 :(得分:2)

您的第二组规则是隐式允许。它在没有重定向或禁止访问的情况下通过请求。这意味着你给它一个你“想要”的条件列表。此外,您匹配的“图像”看起来像是查询字符串,它不属于%{REQUEST_URI}变量的一部分。您需要检查%{QUERY_STRING}变量。

类似的东西:

// If referral is from my domain and accessing images, do nothing
RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|Baiduspider) [NC] //If user agent isn't bot
RewriteCond %{HTTP_REFERER} ^$ [OR]                                  //Allow blank referral
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?mydomain.com [NC]    //if referral is from my domain
RewriteCond %{QUERY_STRING} (^|&)images.*?(&|$)                      //if URL contains string "images"
RewriteRule ^.*$ - [NC,L]                                            // DO nothing 

// If referral is NOT from my domain and accessing images, show watermarked image
RewriteCond %{HTTP_USER_AGENT} !(googlebot|bingbot|Baiduspider) [NC] //If user agent isn't bot
RewriteCond %{HTTP_REFERER} !^$                                      //Allow blank referral
RewriteCond %{HTTP_REFERER} !mydomain.com                            //if referral is NOT from my domain
RewriteCond %{QUERY_STRING} (^|&)images.*?(&|$)                      //if URL contains string "images"
RewriteRule ^images/(.*)$ http://www.mydomain.com/cache/$1 [NC,R,L]  //redirect to watermarked image

如果images事实上是请求URI的一部分而不是查询字符串,那么你就无法拥有&,这就是你要匹配的东西。正则表达式(^|&)images(&|$)匹配图片& images 图片& ,或& images& 没有别的,只有那四种可能性。如果你想要匹配一个图像目录,那么你想要这样的东西:

RewriteCond %{REQUEST_URI} /images/

RewriteCond %{REQUEST_URI} !/images/