1

我即将推出一个摄影网站,并希望为我的图像提供一些保护。我在网上搜索的结果如下:

#Set FollowSymLinks, in most cases already set on the server
Options +FollowSymLinks
#Enable Indexes
Options +Indexes
#Turn on the Rewrite Engine
RewriteEngine on
#Allow my domain
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?mydomain.com [NC]
#Allow another domain
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?anotherdomain.com [NC]
#Allow blank referrers or delete this line to block them
#Deleting this line also blocks access to images by filepaths
RewriteCond %{HTTP_REFERER} !^$
#Allow search engines
RewriteCond %{HTTP_REFERER} !google. [NC]
RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
RewriteCond %{HTTP_REFERER} !msn. [NC]
RewriteCond %{HTTP_REFERER} !yahoo. [NC]
#File types to be blocked
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
#OR
#First allow an image to be hotlinked to avoid looping
#RewriteCond %{REQUEST_URI} !^mydomain.com/image/hotlinker.gif$
#Then display it as a custom image
#RewriteRule .(jpg|jpeg|png|gif)$ mydomain.com/image/hotlinker.gif [NC,R,L]

1) 有没有办法通过不涉及阻止空白引用者的文件路径条目来阻止图像被调用?我有几个我想这样做的实例,但不是整个站点。

2) 我是否拥有允许 Google、MSN 和 Yahoo 正确访问我的图像的正确代码?

3)我拥有的代码是从多个来源合并的。由于我对语法一无所知,我想知道为什么只有第一个没有替代图像的 RewriteRule 以 \ 而不是第二个开头?

感谢任何反馈,谢谢。

4

2 回答 2

3

1) 有没有办法通过不涉及阻止空白引用者的文件路径条目来阻止图像被调用?我有几个我想这样做的实例,但不是整个站点。

我认为没有办法,但您可以在 RewriteCond 中列出例外情况以获得一组单独的规则:

# Check to see if referer is blank
RewriteCond %{HTTP_REFERER} ^$
# Check to see if it isn't one of the files we're going to allow blank referers
RewriteCond %{REQUEST_URI} !^/images/allow_blank_referer/
RewriteCond %{REQUEST_URI} !profile_picture\.png$
# Block images
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

该示例将阻止所有空白引用者,除了来自任何请求图像/images/allow_blank_referer/和任何以profile_picture.png.

2) 我是否拥有允许 Google、MSN 和 Yahoo 正确访问我的图像的正确代码?

不,您的 .htaccess 中有这些行:

RewriteCond %{HTTP_REFERER} !google. [NC]
RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
RewriteCond %{HTTP_REFERER} !msn. [NC]
RewriteCond %{HTTP_REFERER} !yahoo. [NC]

只需允许加载从 google/msn/yahoo 及其搜索缓存链接的图像的人。例如,如果有人在 google 上进行图片搜索并点击您的一张图片,您不会阻止他们。

为了确保 google、msn 和 yahoo bot 不会被阻止,您想要做的是检查 HTTP_USER_AGENT,例如,在您可以插入的那些重写条件下:

RewriteCond %{HTTP_USER_AGENT} !googlebot [NC]
RewriteCond %{HTTP_USER_AGENT} !msnbot [NC]
RewriteCond ${HTTP_USER_AGENT} !slurp [NC]

如果用户代理中存在这三样东西中的任何一个(slurp = Yahoo!),这些将绕过引用者检查,您可以从http://www.robotstxt.org/db.html之类的地方获取用户代理列表

3)我拥有的代码是从多个来源合并的。由于我对语法一无所知,我想知道为什么只有第一个没有替代图像的 RewriteRule 以 \ 而不是第二个开头?

第一个有一个“\”来转义“.”。这 ”。” 在正则表达式中匹配任何东西,有点像通配符,“\”转义它,意思是“我真的是指句点而不是通配符”。注释掉的 RewriteRule 缺少“\”可能是因为有人懒惰。例如,它仍然会匹配以 结尾的请求.gif,但它也会匹配以结尾的请求,zgif而第一个 RewriteRule 则不会。如果您要使用注释掉的 RewriteRule(如果这样做,请务必注释掉RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]),我建议您在第一个句点前放置一个“\”。

于 2011-11-03T19:50:02.590 回答
0

所以我在根目录中的更正代码看起来像这样。

#Set FollowSymLinks, in most cases already set on the server
Options +FollowSymLinks
#Enable Indexes
Options +Indexes
#Turn on the Rewrite Engine
RewriteEngine on
#Allow my domain
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?mydomain.com [NC]
#Allow another domain
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?anotherdomain.com [NC]
#Allow blank referrers or delete this line to block them
#Deleting this line also blocks access to images by filepaths
RewriteCond %{HTTP_REFERER} !^$
#Allow users to follow images from search engines
RewriteCond %{HTTP_REFERER} !google. [NC]
RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
RewriteCond %{HTTP_REFERER} !msn. [NC]
RewriteCond %{HTTP_REFERER} !yahoo. [NC]
#Ensure search engines aren't blocked from indexing images (slurp is yahoo)
RewriteCond %{HTTP_USER_AGENT} !googlebot [NC]
RewriteCond %{HTTP_USER_AGENT} !msnbot [NC]
RewriteCond ${HTTP_USER_AGENT} !slurp [NC]
#File types to be blocked
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
#OR
#First allow an image to be hotlinked to avoid looping
#RewriteCond %{REQUEST_URI} !^mydomain.com/image/hotlinker.gif$
#Then display a custom image
#RewriteRule \.(jpg|jpeg|png|gif)$ mydomain.com/image/hotlinker.gif [NC,R,L]

关于我的问题 1),我的场景是一个专用文件夹,其中包含一些我试图提供一些保护的图像(我已经完成了所有明显的 html 和 javascript 内容,我知道这些内容并不完全证明)。

如果我将 .htaccess 放在那里以停止图像缓存或被索引(尽管我的 robots.txt 应该阻止这种情况),是否也可以检测到非空白引荐来源并仅对它们应用文件路径限制?只留下可以通过文件路径检索图像的空白引用者。

我的 .htaccess 看起来像这样,取决于我上面的问题:

<FilesMatch "\.(jpg|jpeg|png|gif|bmp)$">
Header set Cache-Control: "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0"
</FilesMatch>
#Set FollowSymLinks, in most cases already set on the server
Options +FollowSymLinks
#Enable Indexes
Options +Indexes
#Turn on the Rewrite Engine
RewriteEngine on
#Allow my domain
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?mydomain.com [NC]
#TEST HERE IF THE USER IS NOT A BLANK REFERRER
?????????????????????????????
#IF NOT BLOCK ACCESS TO IMAGES BY ENTERING FILEPATH
RewriteCond %{HTTP_REFERER} ^$
#File types to be blocked
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

我希望这是有道理的。谢谢

于 2011-11-04T14:06:24.677 回答