APACHE 2 - Force download requested files

The goal of this change is to add the following line(s) into the headers of your file(s):

Content-Disposition: attachment

This change will force download the requested file

 

How to do that?

1) Enable mod_headers module (it is usually disabled by default). In Debian and Ubuntu Linux distributions use the following command:

a2enmod headers

For Linux distributions or other Unix systems without additional module handling infrastructure add the following line to web servers httpd.conf:

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so

2) Then we need to tell the web server to send specific headers for those specific situations.

Open your virtual server configuration:

vi /etc/apache2/sites-available/

For example to make all pdf and jpg files to be downloaded instead of open in browser add the following code to your configuration:

<FilesMatch "\.(pdf|jpg)$">

Header set Content-Disposition attachment

</FilesMatch>

Please, make sure that you don't have any duplicate headers before the last step.

3) Restart your web server for configuration to take effect with following command:

/etc/init.d/apache2 restart