Forcing a Download with Apache and .htaccess

Posted February 21st @ 10:34 am  |  Filed in: Apache    

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, simple 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.

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login

Options:

Size

Colors