How to force file downloading with htaccess   July 1st, 2009

Nearly every browser will show image oder video files you want to provide for download in the browser itself instead of opening a download dialog.

Most of the websites suggest to use this code in your .htaccess:

<FilesMatch "\.(jpg|zip|avi)$" >
	ForceType application/octet-stream

But this is not enough! Even when the MIME-Type is set to “octet-stream”, some browsers will still open the files their selfs because they have detected the .jpg or .avi file extension.

The solution to this is to add the following header:

<FilesMatch "\.(jpg|zip|avi)$" >
	ForceType application/octet-stream
	Header add Content-Disposition "attachment"

With this header every browser i tested opened a download dialog, regardless of which file extension is present.

To use this, your webserver has to have mod_headers activated (It is most likely already included in Apache, but not activated): Apache Module mod_headers

Tags: , , , , ,
This entry was posted on Wednesday, July 1st, 2009 at 19:34 and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.

17 Responses

October 5th, 2011 at 01:19
Mamun Says:

I used the below one for force PDF download. I am using Apache 2.2.17. But I got an internal server error (500).

ForceType application/octet-stream
Header add Content-Disposition “attachment”

Can anyone help me regarding this.

November 23rd, 2011 at 13:06
TimeWaster Says:

I am afraid i can’t help you with this, it works fine for me. Is there maybe an error in the FilesMatch directive?

January 13th, 2012 at 19:04
Yasin Says:


it didn’t work first time i tried, then i cleared the browser cache and it worked. This is a very good solution.
It would be interesting, if it works with IE. Im using FF on Mac OSX

Thanks for sharing this.

January 14th, 2012 at 06:16
TimeWaster Says:

yes, it works with IE. i used it in a big portal page without errors.

January 29th, 2013 at 16:06
invisible Says:

it worked fine on firefox, google chrome and ie

February 5th, 2013 at 20:11
Adam Says:

Which browsers require the 2nd line, ‘Header add Content-Disposition “attachment”‘ for the automatic file download prompt to work? If it’s IE7 and below, then to heck with them. :) Thanks for the code!

February 5th, 2013 at 20:15
TimeWaster Says:

Most of them. Especially Firefox.

February 5th, 2013 at 20:21
Adam Says:

Thanks for your response. I just tested with a site of mine, and the whole site gives me a 404 error unless I remove that 2nd line. The “force download” works with that line removed (I’m using FF 18.0). Any thoughts why it works better without that 2nd line?

February 5th, 2013 at 20:36
TimeWaster Says:

the only things coming to my head are: clear browser cache, make sure that the copied text does not consist of malicious chars, check if this interferes with your own config.

other than that i have no idea what is happening. try it without the 2nd line, but test it in every browser to be sure…

February 5th, 2013 at 20:43
Adam Says:

Clearing the cache didn’t work. I’ll test it in various browsers when I get the chance. Thanks for your help!

February 27th, 2013 at 19:11
Loren Says:

Is it possible to target a specific browser (Firefox 19) with this approach?

February 28th, 2013 at 06:51
TimeWaster Says:

I have no idea. Take a look in the apache documentation, if there is a way to check for browsers it will be described there.

February 28th, 2013 at 18:40
Loren Says:

Thanks for the quick response. I’ll do that.

August 21st, 2013 at 16:13
Simpleguy Says:

You need to enable Apaches ‘headers_module’.

September 16th, 2013 at 04:45
Jay Says:

Hey everyone having 500 errors. The 2nd line requires the Apache Mod_Headers. So either add it yourself if you’re on a VPS or get on the phone to your webhost!

Good luck, J

September 17th, 2013 at 16:17
shankar Says:

it’s showing Internal Server Error anyone help?

September 17th, 2013 at 16:27
TimeWaster Says:

@shankar: take a look at the updated article above.

Leave a Reply