1 Jul

How to force file downloading with htaccess

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

20 thoughts on “How to force file downloading with htaccess

  1. 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

  2. Pingback: php – How do I force a jpeg image to download instead of open in a browser window? | Asking

  3. Pingback: Htaccess Force Download

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.