Setting a filename inside the header with htaccess

眉间皱痕 提交于 2019-12-01 14:42:11
Gerben

I have no idea if it works, but this would be my first attempt.

RewriteRule [^/]+\.pdf$ - [E=FILENAME:$0]
<FilesMatch "\.(?i:pdf)$">
    Header set Content-Type application/octet-stream
    Header set Content-Disposition "attachment; filename=%{FILENAME}e" 
</FilesMatch>

EDIT Seems some apache installs prefix the env variable with REDIRECT_

RewriteRule [^/]+\.pdf$ - [E=FILENAME:$0]
Header set Content-Type application/octet-stream env=REDIRECT_FILENAME
Header set Content-Disposition "attachment; filename=%{REDIRECT_FILENAME}e" env=REDIRECT_FILENAME

Above code doesn't work out of the box. In my case, I needed another internal redirect.

But why would you need to set the filename in the header if the request URL contains the filename too? All browsers will save the file with under the name in the URL. So going to /path/test.pdf will result in browsers suggesting the filename test.pdf.

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