syncopated syncopated - 1 month ago 5
Apache Configuration Question

mod_rewrite help to change Content-disposition based on 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 -- in this case, I just want to serve the file and let the browser play it.

/media/download/file1.mp3 -- in this case, I want to make it easy for a user to download the file instead.

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.


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]

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