【问题标题】:Force download mime type if has a certain parameter in url如果 url 中有特定参数,则强制下载 mime 类型
【发布时间】:2017-04-29 15:15:04
【问题描述】:

我知道我可以强制下载特定文件夹中的所有文件或下载具有指定扩展名的所有文件。例如:

<FilesMatch "\.(mp3|avi)$" >
    ForceType application/octet-stream
    Header add Content-Disposition "attachment"
</FilesMatch>

但如果请求 url 中给出了参数,我想强制下载文件,例如?download=true。我该怎么做?

【问题讨论】:

  • 使其嵌套&lt;If "%{QUERY_STRING} =~ /download=true/"&gt;&lt;/If&gt;。每行一个指令。
  • @Deadooshka 实际上,我正在寻找合适的 if 条件!

标签: apache mime-types


【解决方案1】:

很高兴能够在纯静态网站上基于 URL 自定义 HTTP 标头,而无需任何动态脚本语言及其安全问题。

标题的完整性检查适用性

  • Content-Disposition 在RFC6266 中进行了描述。
  • RFC2616 中描述了 HTTP 上下文中的 Content-Type。

理论上只有 Content-Disposition “附件”是必要的。

解决方案

将问题中的信息和第一条评论结合起来对我有用:将下面的行放在包含文件的目录或上方的.htaccess 中。

<If "%{QUERY_STRING} =~ /download=true/">
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</If>

这在Expressions in Apache HTTP Server 中有记录,似乎不需要任何特定的 Apache 模块。

协议级测试

之前

curl -o /dev/null -v https://some.url/example.jpg 2>&1 | grep " Content"

< Content-Length: 1201533
< Content-Type: image/jpeg

curl -o /dev/null -v https://some.url/example.jpg?download=true 2>&1 | grep " Content"

< Content-Length: 1201533
< Content-Type: image/jpeg

之后

curl -o /dev/null -v https://some.url/example.jpg 2>&1 | grep " Content"

< Content-Length: 1201533
< Content-Type: image/jpeg

curl -o /dev/null -v https://some.url/example.jpg?download=true 2>&1 | grep " Content"

< Content-Length: 1201533
< Content-Disposition: attachment
< Content-Type: application/octet-stream

实际测试

结论

成功了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2018-11-29
    • 2012-02-27
    相关资源
    最近更新 更多