使用< filesMatch>强制下载在htaccess中 [英] Forcing a download using <filesMatch> in htaccess

查看:107
本文介绍了使用< filesMatch>强制下载在htaccess中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图强制下载一个文件夹中的所有文件。

I'm trying to force the download of all files of one folder.

页面上的链接看起来像这样

The link on the page looks like this

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

我已经知道标记内的两行有效,因为当我放置.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.

推荐答案

请查看文件匹配文件。它明确指出


本节中给出的指令将应用于具有基名(文件名的最后一个部分)的任何对象
匹配指定的
文件名。

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 匹配,但是您的第一个示例。*上载/ 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.

您应该附上<文件*。*> <文件* .pdf> (取决于您要强制执行的下载内容)在 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.

这篇关于使用&lt; filesMatch&gt;强制下载在htaccess中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆