filesMatch>使用&LT强制下载; htaccess的在WWW根 [英] Forcing a download using <filesMatch> in htaccess at WWW root

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

问题描述

我试图迫使一个文件夹中的所有文件的下载。

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>

我已经知道,标签内的2号线工程,因为它的工作原理当我把一个直接的.htaccess,我想强制下载有以下code里面的文件夹:

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 ,但是你的第一个例子。*上传/文件。* 没有。它实际上不可能匹配,因为它包含斜线,它被用来作为目录分隔符

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.

您应该封闭或者&LT;文件*&GT; &LT;文件的* .pdf&GT; (这取决于你要强制下载)的位置是什么指令:

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文件允许的。只要创建一个htaccess的在你的 /上传/文件/ 目录。

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

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