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.

3 Comments

  1. http://www.10thpla.net » Andy said on November 28, 2008 at 08:46...

    Broke our server with a 500 error.

    Removing the Header set line stopped it.

  2. http://www.ngenworks.com » Joey Marchy said on February 12, 2009 at 11:40...

    For those Expression Engine users out there, this did not work for me until I moved the rules into the .htaccess file in the root.

    Placing an .htaccess file in the folder that contained the images was not effective.

  3. http://zantor.mail.ua » zantor said on May 6, 2010 at 06:39...

    You need to:

    LoadModule headers_module modules/mod_headers.so

    ForceType application/octet-stream

    Header set Content-Disposition attachment

    Works fine.

Leave a comment

OpenID Login

Standard Login

Options:

Size

Colors