强制使用.htaccess文件下载 [英] Force file to download with .htaccess

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

问题描述

所以我在htaccess中使用以下规则:

So I am using the following rule in the htaccess:

AddType SCHM wsc

<FilesMatch "\.(wsc)$">
  ForceType SCHM
  Header set Content-Disposition attachment
</FilesMatch>

当我转到文件的位置时,它不会强制下载

But when I go to the file's location it doesn't force the download

推荐答案

由于注释中已经回答了该问题,因此仅以Stackoverflow如何指定它的方式提供答案。

Since the question is already answered in the comments, this is just to provide an answer in the way how Stackoverflow designated it.

就像问题一样,可以使用 Apache 2 mod_headers 来解决。由于 Content-Disposition 不是 HTTP 标准的一部分,因此您可以添加其他标头来实现目标。

Like in the question it can be solved by using mod_headers of Apache 2. Since Content-Disposition is not part of the standard of HTTP, you may add some other header to achieve your objective.

<FilesMatch "\.(wsc)$">
    Header set Content-Type application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>

您应该考虑的另一件事是您的浏览器可能会缓存服务器的响应。浏览器仍将发送请求,但是该请求将包含一个节点,该节点浏览器已经具有给定日期的文件。如果自给定日期以来文件没有更改,则服务器不会将新标头发送到您的浏览器。这意味着,如果更改 .htaccess ,在禁用浏览器中的缓存或更改文件的时间戳之前,可能看不到任何影响。

Another thing you should consider is that your browser may cache the responce of the server. The browser will still send the request, but the request will contain a node that the browser already have the file from a given date. If the files hasn't changed since the given date, the server will not send the new headers to your browser. This means if you change the .htaccess, you may not see any impact until you disable caching in your browser or you change the timestamps of the file.

您还可以添加

Header set X-Content-Type-Options "nosniff"

具有更好的兼容性(也许还有安全性)。它可以防止浏览器进行MIME类型的嗅探,该方法将忽略声明的内容类型。有关更多信息,请参见此处

for better compatiblity (and maybe security). It prevents the browser from doing MIME-type sniffing, which would ignore the declared content-type. See here for more information.

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

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