Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Content-Disposition header to attachment only on files in a certain directory?

I've got this this rule in my htaccess file to force linked files to download rather than open in the browser:

<FilesMatch "\.(gif|jpe?g|png)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

Is there a way to alter the RegExp so it only applies to files in a certain directory?

Thanks

like image 503
DonutReply Avatar asked Oct 20 '10 11:10

DonutReply


People also ask

How do you use content disposition attachment?

In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

What is content disposition attachment filename?

Content-Disposition is an optional header and allows the sender to indicate a default archival disposition; a filename. The optional "filename" parameter provides for this. This header field definition is based almost verbatim on Experimental RFC 1806 by R. Troost and S.

What is content disposition inline?

1. Content Disposition Type : inline: This indicates that data should be displayed automatically on prompt in browser. attachment: This indicates that user should receive a prompt (usually a Save As dialog box) to save the file locally on the disk to access it.


2 Answers

You will probably need to put the directives in the .htaccess file in the particular directory.

like image 54
Gumbo Avatar answered Nov 12 '22 14:11

Gumbo


Like @gumbo said, put the .htaccess file in the highest level folder you want to affect. and those settings will trickle down to sub folders. You may also want to make sure the headers module is enabled before using this in your htaccess file. The following line will generate an error if the headers module is not enabled:

Header set Content-Disposition attachment

here's an example that forces download of mp3 files only if the headers module is enabled:

<IfModule mod_headers.c>
    <FilesMatch "\.(mp3|MP3)$">
        ForceType audio/mpeg
        Header set Content-Disposition "attachment"
        Allow from all
    </FilesMatch>
</IfModule>

Note: it does not enable the module, it just ignores anything inside the IfModule tags if the module is not enabled.

To enable apache modules you'll either need to edit your httpd.conf file or in wamp server you can click the wamp tray icon and select "Apache -> Apache Modules -> headers_module" or make sure it is checked.

like image 36
TxRegex Avatar answered Nov 12 '22 13:11

TxRegex