.htaccess command to force the files to download
If you are not familiar with .htaccess and hearing it for the first time, here’s some info. If your website is running on a linux/apache based shared web server, your web server is sure to support the .htaccess file. That said, this will not work for windows based servers as the architecture and commands are all different for windows. Enough of into, follow the procedure below to set the files to download.
- Check if the folder containing your files already has a .htaccess file or not. If using cPanel, you will be needed to check the check-box to show hidden files. If there exists one, open it for editing. Otherwise create a new file with the name .htaccess in that particular directory and open it for editing.
- Depending on whether you are editing the existing file or creating a new one, paste the following code towards the top of the file. You can choose other location if the top includes some more important commands.
<FilesMatch "\.(?i:doc|odf|pdf|rtf|txt)$"> Header set Content-Disposition attachment </FilesMatch>
- Now you need to configure the above command to make sure the extension of your particular file is included in it. For example if you want to force a verisign.cer file to always download, the code will change to the one shown below. As a note, you need to remove the extensions of the files which you want to not to be always downloaded and let the browser choose what to do.
<filesmatch "\.(?i:doc|odf|pdf|cer|txt)$"> Header set Content-Disposition attachment </FilesMatch>
We have removed the extension called rtf and replace it with cer. But it’s not important to remove the existing ones. You can add a new extension as |ext in the command.
- Save the file when you are done editing/configuring.
- Clear your cache and try the direct links to the files of that particular extension. They’ll download instead of opening in the browser.