使用 <filesMatch> 强制下载在 htaccess 中 [英] Forcing a download using <filesMatch> in htaccess
问题描述
我正在尝试强制下载一个文件夹中的所有文件.
I'm trying to force the download of all files of one folder.
页面上的链接是这样的
<a href="http://example.com/uploads/documents/file.pdf">Click to download</a>
我的 .htaccess 中有这个片段
And I have this snippet in my .htaccess
<filesMatch ".*uploads/documents.*">
ForceType application/octet-stream
Header set Content-Disposition attachment
</filesMatch>
我已经知道标签内的 2 行是有效的,因为当我将 .htaccess 直接放在我想使用以下代码强制下载的文件夹中时它有效:
I already know that the 2 lines inside the tag works, because it works when I put a .htaccess directly inside the folder where I want to force the download with the following code:
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
我似乎不了解 filesMatch 标签.
There seems to be something which I don't understand about the filesMatch tag.
推荐答案
请查看 FilesMatch 和 文件, 分别.它明确指出
Please look at the documentation for FilesMatch and Files, respectively. It clearly states
本节中给出的指令将应用于任何对象具有与指定的匹配的基本名称(文件名的最后一个组成部分)文件名.
The directives given within this section will be applied to any object with a basename (last component of filename) matching the specified filename.
这意味着在您的示例中,它与 file.pdf
匹配.您的第二个示例 *.*
匹配 file.pdf
,但是您的第一个示例 .*uploads/documents.*
不匹配.它实际上永远无法匹配,因为它包含一个斜杠,用作目录分隔符.
That means that in your example it matches against file.pdf
. Your second example *.*
matches file.pdf
, however your first example .*uploads/documents.*
does not. It actually can never match, since it contains a slash, which is used as a directory separator.
您应该将
或
(取决于您要强制下载的内容)包含在 Location
指令:
You should enclose either <Files *.*>
or <Files *.pdf>
(depending on what you want to enforce downloading) in a Location
directive:
<Location "/uploads/documents/">
<Files *.*>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>
</Location>
如果您无法编辑 apache 配置
不幸的是,在 .htaccess 文件中不允许使用 Location 指令.只需在 /uploads/documents/
目录中创建一个 .htaccess.
If you cannot edit the apache config
Unfortunately, the Location directive is not allowed inside .htaccess files. Just create a .htaccess inside your /uploads/documents/
directory.
这篇关于使用 <filesMatch> 强制下载在 htaccess 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!