2011-11-03 53 views
1

我即將啓動一個攝影網站,並希望對我的圖像提供一些保護。我有以下爲在網絡上搜索的結果:使用htaccess進行圖像保護

#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)我是否有正確的代碼允許谷歌,MSN和雅虎正確訪問我的圖片?

3)我擁有的代碼是從多個源合併而成的。由於我對語法沒有任何瞭解,所以我想知道爲什麼只有沒有替代映像的第一個RewriteRule以\開頭,而不是第二個?

感謝任何反饋,謝謝。

回答

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)我是否有正確的代碼允許谷歌,MSN和雅虎正確訪問我的圖片?

沒有,這些線在你的.htaccess有:

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

簡單地讓人們誰加載從谷歌/ MSN /雅虎自己的搜索鏈接緩存的圖像。例如,如果有人在Google上進行了圖片搜索並點擊了其中一張圖片,則不會阻止他們。

你想要做什麼,以確保谷歌,微軟和雅虎機器人不會被阻塞是檢查HTTP_USER_AGENT,例如,下面的重寫條件可以插入:

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

那些將繞過引薦檢查是否有任何的那些三件事是存在於用戶代理(啜食=雅虎),你可以從地方獲取用戶代理的列表,像http://www.robotstxt.org/db.html

3)我的代碼合併來自不止一個來源。由於我對語法沒有任何瞭解,所以我想知道爲什麼只有沒有替代映像的第一個RewriteRule以\開頭,而不是第二個?

第一個有「\」來轉義「。」。 「。」在正則表達式中匹配任何東西,有點像通配符,而「\」則轉義它,意思是「我的意思是指期間而不是通配符」。註釋掉的RewriteRule缺少「\」可能是因爲有人懶惰。它仍然會匹配以.gif結尾的請求,但它也會匹配以zgif結尾的內容,而第一個RewriteRule則不會。如果你打算使用註釋掉的RewriteRule(如果你這樣做,一定要註釋掉RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]),我建議在第一個時間段前放一個「\」。

+0

感謝喬恩的幫助,我很感激。我已經發布了一些更多的回答我的問題。 – martin

+0

如果我沒有遵循正確的協議回答我自己的問題,我會道歉嗎?我沒有看到我如何能夠把它作爲一個評論。 – martin

+0

依靠HTTP_REFERRER安全嗎? – Geo

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] 

我希望這是有道理的。謝謝

+0

您可以將.htaccess放入受保護的文件夾中,只會影響該文件夾。你發佈的那個看起來很好。 –