How to Force File Downloads with the Htaccess File

When you use an Htaccess file, you’re basically giving special instructions to the Apache web server itself. This is especially important in our shared server hosting packages, where the user does not have access to the Apache configuration files. You can pass many of the same directives through the Htaccess file itself without requiring root access.

Why Force File Downloads

By default, when users visit your page and click on a PDF or an audio file, it will be open up in the web browser itself. This works for most cases, but if your goal is to have your users actually download the file, you can make it easier for them by forcing the download as soon as they click the link.

How to Force File Downloads with the Htaccess File

First, you will need to make sure that you edit the right file. Usually, the .htaccess file will be located in the document root of the site it is affecting. However, sometimes an Htaccess file will be placed in the cPanel user’s home directory meant to recursively affect directories beneath it.

If you have a WordPress site, your Htaccess file be installed in the same directory as all of the other WordPress core files.

In order to proceed, you will need a sensible way to edit files. For adding Htaccess directives, the easiest way to get to the file will be through the cPanel file manager.

Once you’ve opened the Htaccess file for editing, add these snippet of code, adding any file extensions you require:

 AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

Source: WordPress.org

Htaccess Tips and Tricks

As mentioned above, making best use of the Htaccess files is especially important in our Business Class hosting accounts. There’s a lot you can do without requiring the elevated responsibilities of root user access. The Htaccess file is a bridge between the default cPanel user account and the deeper levels of the Apache configuration.

We’d love to hear any tips or tricks you may have for neat things you can do with the Htaccess file. Be sure to leave them in the comment section below.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

7 thoughts on “How to Force File Downloads with the Htaccess File

    1. I’m sorry to see you are facing a problem. Can you please provide any error or which step in the process outlined in this article you are experiencing issues with? This will help us to assist you in finding a resolution. If you are a customer of InMotion Hosting, you can always contact our Live Technical Support for more personal assistance with your account.

  1. You can use the AddType code in your .htaccess file, but it will not “work” if the browser reading the code doesn’t have a device assigned to play the file type. The code is used to help bind the file extension to a MIME type. You can see more information on it here. As long as you have a device/application assigned to play the file type, then the browser will be able to play when the page is read. Otherwise, it will require code built into the page to handle how the file will be treated. Each browser handles (and refers to) file types differently. For example, Firefox (the browser I’m using) requires that you go to Preferences, then scroll down to Applications. Please refer to your browser documentation in order to find out more information. If you continue to have problems with your code, then you may need to consult with an experienced web developer for more information.

  2. I’ve add
    AddType application/octet-stream .mp3
    To our .htaccess file and it does not work. I had other server provider and it worked without problems, but I don’t know why here does not let download mp3 files links instead of stream them.
    Any sugestion? Thanks ind advance.

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X