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 can 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
- Log into your cPanel.
- Find the Files category and click on the File Manager icon.
- A popup box will appear. For the primary domain, click on the Web Root radio button. For addon domains, click on the dropdown and find your desired addon domain name. Be sure the checkbox next to Show Hidden Files is checked. Click the Go button to enter the File Manager.
- 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.
- 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
- Be sure to hit the Save Changes button in the upper right corner to save your new htaccess configuration.