Force file to download with .htaccess

不想你离开。 提交于 2020-01-06 03:02:14

问题


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


回答1:


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

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>

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.


You can also add

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

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.




回答2:


RFC2616 say for 19.5.1 Content-Disposition

If this header is used in a response with the application/octet- stream content-type, the implied suggestion is that the user agent should not display the response, but directly enter a `save response as...' dialog.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1



来源:https://stackoverflow.com/questions/33187793/force-file-to-download-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!