Javascript Tricks, Samples, Tutorials & Howtos
Last updated: 06 Sep 2010

Using Apache 2 configuration to add Content-disposition header

Apache 2 web server has a header manipulation module (mod_headers) that allows adding required headers to HTTP response.

Adding Content-disposition header through web server configuration is easy (albeit not always possible if the need to store the file is dictated by a web application).

What to change in server configuration

  • Enable mod_headers module (it is usually disabled by default). In Debian and Ubuntu Linux distributions this is done using the following command:
    a2enmod headers
  • For Linux distributions or other Unix systems not having any additional module handling infrastructure this can be done by adding the following line to web servers httpd.conf:
    LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
  • Then we need to tell web server to send Content-Disposition header for certain situations, for example to make all pdf files to be downloaded instead of open in browser add the following code to your virtual server configuration:
    <FilesMatch "\.pdf$">
        Header set Content-Disposition attachment
    </FilesMatch>
This will suffice for most modern browsers which will are able to derive the filename from URL by stripping parameters and path information.
  • Restart your web server for configuration to take effect.