Forcing a Download with Apache and .htaccess

Sometime you don't want to give your site visitors the option to open a file in their web browser. This could be a Microsoft Office document, or even a giant jpg or png image. When you want to force download a certain file type, simply add something like this to an .htaccess file in the directory that the file(s) are located in:

<Files *.xls> ForceType application/octet-stream Header set Content-Disposition attachment </Files> 
<Files *.eps> ForceType application/octet-stream Header set Content-Disposition attachment </Files>

This particular directive forces downloads all .xls and .eps files (the * means match any file name, followed by the extension of the file) in the same directory, as well as any sub-directories, and ensures that the browser will not try to open the file.

In general, I like to avoid putting such things in my root .htacess file, preferring to create a "downloads" directory of some sort, and just adding the above lines as needed. Some servers are different, so your mileage may vary - check the comments below.

This entry was posted in Apache and tagged , . Bookmark the permalink.

18 Responses to "Forcing a Download with Apache and .htaccess"

Leave a reply