mod_rewrite帮助更改基于URI的内容处置 [英] mod_rewrite help to change Content-disposition based on URI
问题描述
我有一个mp3文件目录,希望能够直接为它们提供服务,或者为用户提供基于请求URI的下载选项.
I have a directory of mp3 files want to have be able to serve them inline or giving the user an option to download based on the request URI.
/media/file1.mp3-在这种情况下,我只想提供文件并让浏览器播放.
/media/file1.mp3 -- in this case, I just want to serve the file and let the browser play it.
/media/download/file1.mp3-在这种情况下,我想让用户更轻松地下载文件.
/media/download/file1.mp3 -- in this case, I want to make it easy for a user to download the file instead.
我已经能够使用mod_rewrite和php(使用header()和readfile()函数)完成此操作,但是我更愿意使用mod_rewrite,mod_header等来完成所有操作.
I have been able to accomplish this with mod_rewrite and php (using the header() and readfile() function) but I would rather do it all with mod_rewrite, mod_header etc if possible.
推荐答案
使用mod_rewrite,您只能更改某些特定的标头字段,但 Content-Disposition 标头字段不属于该标头字段.您只能更改 Content-Type 标头字段:
With mod_rewrite you can only change some specific header fields but to which the Content-Disposition header field doesn’t belong. You could only change the Content-Type header field:
RewriteRule ^media/[^/]+\.mp3$ - [L,T=audio/mpeg]
RewriteRule ^media/download/[^/]+$ - [L,T=application/octet-stream]
如果您要使用 mod_headers + mod_setenvif 解决方案:
And if you want to use a mod_headers+mod_setenvif solution:
SetEnvIf Request_URI ^/media/download/ force-download
<IfDefine force-download>
Header set Content-Disposition attachment
Header set Content-Type application/octet-stream
</IfDefine>
这篇关于mod_rewrite帮助更改基于URI的内容处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!