Forcing a file to download in a browser via htaccess

The .htaccess file is a hidden text file within your hosting account that can be very powerful. It is designed so that you can modify it to change the behavior of your website that normally would take higher access beyond your account. It acts as a liason between your domain and the server and can perform many functions.

When you design a site, you may offer a link to a file for your visitors. This may be a document, an audio file, or a video file. The visitor's browser can be configured to deal with the file in different ways. It cean either open the file in the browser, provided it has the proper plugin, or it can offer the visitor to download the file.

You may want to always have the user download the file instead of opening it within their browser. This way the user has their own copy of the file. To do so requires a small bt of code to be added to your htaccess file for that domain. Below is the set of instructions to help you do just that.

How to force a file fo download instead of displaying via the .htaccess file in cPanel

Step 1. Log into your cPanel.

Step 2. Click on the File Manager icon.

Step 3. Make sure you are able to view hidden files.

Step 4. You should now be in the root folder of the domain you chose. Look for the .htaccess file and right click on it. This brings up a menu. Find and click on the Edit option. If you get a popup box, simply find and click the Edit button in the lower right corner to continue to the editor.

Step 5. You are now in the text editor. Place the following code into your htaccess so that it forces the visitor to download that type of file. You will need to have a line of code for each type of file. It is usually best to place this code at the top of the file.

In the example below, we will give an example for two types of files. The only difference between the lines of code will be the file extension. In our example, we want the visitor to have to download videos with the .mov extension and documents with the .pdf extension.

AddType application/octet-stream .mov
AddType application/octet-stream .pdf

Step 6. Be sure to hit the Save Changes button in the upper right corner to save your new htaccess configuration.

  • htaccess, Forcing download, browser
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Force www or non-www in htaccess

Many customers at some point request to force either the www or non-www version of their site to...

How to Change your Default Index Page in .htaccess

The server looks for specifically named files as the first page of your website, also known as...

How to Add mod_expires to your .htaccess

When visiting a website, the website will cache in your browser. This cache can be controlled by...

Where is my .htaccess File?

In this article we will show you where your account's .htaccess file resides on the server, as...

Removing file extensions using htaccess

Lately, there seems to be a trend where designers want to cloak their file extensions from their...