【问题标题】:Force all files under the specific path to download (not open in browser) using .htaccess使用 .htaccess 强制下载特定路径下的所有文件(不在浏览器中打开)
【发布时间】:2018-10-20 06:05:46
【问题描述】:

我知道如何强制使用 .htaccess 下载所有 jpg 文件,如下所示:

AddType application/octet-stream jpg

但现在我想强制使用.htaccess下载特定路径下的所有文件(无论是什么格式)

例如:

强制/home/user/public_html/files/attachments路径下的所有文件下载,不能直接在浏览器中打开。

我该怎么办?

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    你大概可以使用RewriteRule来达到你的目标,不需要使用AddType

    RewriteCond %{REQUEST_URI} ^/home/user/public_html/files/attachments/(.*)$ [NC]
    RewriteRule ".+\..+" "-" [T=application/octet-stream,L]
    

    仅当路径与/home/user/public_html/files/attachments 匹配时,这将强制在浏览器中下载任何类型的格式。

    【讨论】:

      【解决方案2】:

      强制使用Content-Disposition 标头。

      Header always set Content-Disposition "attachment"
      

      【讨论】:

        【解决方案3】:

        您可以将文件夹中所有可能的扩展添加到 AddType。喜欢,

        AddType application/octet-stream .jpg .doc .mov .avi .pdf .xls .mp4
        

        我遇到了类似的情况,但找不到更好的方法,因此将其发布为答案。

        【讨论】:

          猜你喜欢
          • 2013-04-13
          • 2015-11-12
          • 2015-01-25
          • 2011-06-19
          • 2017-06-03
          • 1970-01-01
          • 2013-10-15
          • 1970-01-01
          • 2014-01-29
          相关资源
          最近更新 更多