Content-Disposition Header - Like A Boss 👔☕

Posted at

##place inside a folder that contains some music videos and documents, this will override the default handler.

<FilesMatch "\\.(mov|mp3|pdf)$">

<IfModule mod_mime.c>
ForceType application/octet-stream
</IfModule>

<IfModule mod_headers.c>
##fix a bug in old GoDaddy servers.
Header unset X-Content-Type-Options
Header unset Content-Disposition

##prevent mimetype sniffing (first few bytes can determine that a file should be opened in browser).
Header set X-Content-Type-Options "nosniff"

##extract filename, apply to proper (for example) Content-Disposition: attachment; file="my music.mp3"
SetEnvIf Request_URI "^.*/([^/]*)$" FILENAME=$1
Header set "Content-Disposition" "attachment;filename=\\"%{FILENAME}e\\""
UnsetEnv FILENAME
</IfModule>

</FilesMatch>



forcing type / because of 'reasons'? 👐
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.



also edit your HTML so links would use HTML5's download attribute:

<a href="mydirectory/my%20music.mp3" download="my music.mp3" type="application/octet-stream" title="click to download">hello!!</a>


(which is good enough for Google).

✨And, naturally.. 👀 you can combine both of the solutions!


reference: W3 / Protocols - RFC2616 (Section 19).





also answered on stackoverflow