簡體   English   中英

使用htaccess限制從其他域訪問zip文件

[英]restricting access to zip files from other domains with htaccess

我正在運行一個網站,該網站在一個文件夾中有許多zip文件。

我希望人們能夠從網站上的下載鏈接(位於登錄后面)訪問這些文件,但我想確保不能通過直接鍵入zip文件的URL來下載它們。

因此,我認為這需要使用htaccess來完成-並拒絕所有規則(我的域除外)。 這是正確的嗎?

現在,我的文件包含以下內容(我的實際域替換為“域”):

SetEnvIfNoCase Referer "^http://domain.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://domain.com$" locally_linked=1
SetEnvIfNoCase Referer "^http://domain.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://domain.com$" locally_linked=1
SetEnvIfNoCase Referer "^$" locally_linked=1
<FilesMatch "\.(zip)$">
  Order Allow,Deny
  Allow from env=locally_linked
</FilesMatch>

有人可以闡明為什么這可能行不通嗎?

我在共享主機(Dreamhost)上:/-但是我認為這並沒有多大區別。

謝謝。

您試圖做的是防止熱鏈接 您無法真正阻止它的發生,但是您可以拒絕其他引薦來源的下載。

將以下內容添加到您的.htaccess文件中,用您的域替換example.com 如果要保護其他文件格式,請在標有zip的位置添加它們,例如(zip|rar)

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)example.com/.*$ [NC]
RewriteRule \.(zip)$ - [F]

如果您要提供其他內容(例如說您必須先登錄的頁面),請使用:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)example.com/.*$ [NC]
RewriteRule \.(gif|jpg)$ http(s)?://www.example.com/alternate.html [R,L]

我有類似的東西,但使用mod_rewrite並使用此代碼

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(zip)$ - [NC,F,L]

您可以使用標頭執行此操作:

// set example variables
$filename = "file_to_download.zip";
$filepath = "/var/www/domain/path_to_download/";

// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);

在此示例中,您可以添加更多登錄名,例如用戶登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM