set content disposition header in apache

How to Set Content-Disposition Header to Attachment in Apache

If you want to enable downloads on your website, you need to need to set content-disposition of your download URLs to ‘attachment’. Otherwise, the requested file may open in the viewer’s browser instead of displaying a ‘Save As’ dialog box. This is especially important for zip, binary and executable files that may not make much sense if opened in browser, instead of being downloaded. Here is how to set content-disposition header to attachment in Apache server.


How to Set Content-Disposition Header to Attachment in Apache

Before you proceed, please ensure you have enabled mod_rewrite (.htaccess) in your Apache server. We will be using .htaccess file for our purpose.


1. Open .htaccess file

Open terminal and run the following command to open .htaccess file.

$ sudo vi /var/www/html/.htaccess


2. Set content-disposition header for all URLs

If you want to set content-disposition header for all downloadable .pdf files on your website, then add the following line to your .htaccess file.

<FilesMatch "\.pdf$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>

In the above code the FilesMatch tag will match all URLs ending with .pdf and apply content-disposition header as ‘attachment’. We also use a ForceType directive to enforce download.

You may also use Files tag instead of FilesMatch above.

<<Files ~ "\.pdf$">>
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</Files>

Both Files & FilesMatch do the same thing but FilesMatch is preferred because it is more intuitive and has simpler syntax.

If you want to set content-disposition header for multiple file types such as .pdf, .html, .mp3, .zip, then use a regular expression as shown below. We use | operator to indicate pdf or html or mp3 or zip

<FilesMatch "\.(pdf|html|mp3|zip)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</FilesMatch>

Please note, since we have added the above lines to .htaccess file in website’s root folder, it will be applicable to all URLs on your website.

If you want to set content-disposition header for URLs pertaining to a specific folder such as /downloads and its subfolders, then create/open .htaccess file in that folder and place the above code in it.

$ sudo vi /var/www/html/downloads/.htaccess


3. Restart Apache Server

Restart apache server to apply changes.

$ sudo service apache2 restart

That’s it. Now if you open browser and visit a download link (e.g https://example.com/test.pdf) for pdf file, the browser will automatically open a ‘Save As’ dialog box without opening the file in browser.

In this article, we have learnt how to set content-disposition header as attachment in Apache web server. If your website or web application requires users to download files, then you may set the header of those URLs as shown above.

Also read :

How to Get Package Details in Ubuntu
How to Force Download in Apache Server
How to Copy to Clipboard in Javascript
How to Check Commands Executed by Users in Linux
How to View User Login History in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *