2

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

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

我非常想创建这个答案的第 2 步,但是我遇到了“等于”和“不等于”的问题,因为我来自 php 和!运算符用于不等于。

帮助别人?

4

1 回答 1

2

您的第二组规则是隐式allow。它通过请求而不重定向或禁止访问。这意味着你给它一个你“想要”的条件列表。此外,看起来您要匹配的“图像”在查询字符串中,它不会成为%{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 的一部分而不是查询字符串,那么您将无法在其中包含 a &,而这正是您要匹配的内容。正则表达式(^|&)images(&|$)匹配images&imagesimages&&images&,仅此 4 种可能性。如果您尝试匹配图像目录,那么您需要这样的东西:

RewriteCond %{REQUEST_URI} /images/

或者

RewriteCond %{REQUEST_URI} !/images/
于 2013-04-16T19:12:54.147 回答