How To Fix Mp3, Mp4, Pdf, and other files opening in browser instead of downloading straight away.
Lately many WordPress users have been facing the issue of wanting people to download your files right away, instead they keep opening in browsers.
Don’t worry I just got a little fix but note; it doesn’t work on every host server.
According to my research, I learnt that this method I’m about to share works on 80% of shared host servers while the remaining 20%, may be by chance the trick/fix ain’t gonna work.
If you’re hosting your website on servers like namecheap, siteground, whogohost, abollyhost, and many more, this is going to work for you.
I do not want to keep this secret to myself which is why I am going to share with you right now.
How To Force A File To Download
Zip The File
- On your pc or smartphone, you can get an archive viewer like zip, which I guess should be able to zip a file aside from viewing.
- Select the file you want to upload on your site, zip it, then upload. The file should be able to download automatically. Note: It’s really not a good idea sometimes… Unlike your audio files, it’s not everybody that knows what it’s like to zip and unzip a file, so when people find it difficult to get your files downloaded in the correct format, they will quit coming to your site.
Through Your .Htaccess
If you are really into website designing and development, you should know what htaccess means.
Htaccess is a file in your website’s file manager and I can say it is what make your site to be able to execute certain commands.
Making changes to htaccess is always not advisable if you do not know what you are doing.
Htaccess in other words, if it is absent you site won’t show up, if you enter the wrong code, you might get certain errors like the “503 error” and so on.
So always be careful whenever you’re dealing with htaccess file.
I won’t treat you as if you’re an expert, an amateur might as well be reading this, sorry for that word, so I have to break it down.
Where can we find the .htaccess?
- Login to your host server’s control panel (Cpanel)
- Go to file manager, click on the public_html, you will see some files in the root. In some server it is always hidden, so if possible attackers got their way into your server they won’t be able to find it talk less of making changes to it. So if you want to make it show, click on the settings button that is about the right top corner, there you will find “show hidden files”.
- Tick, then save.
- Your htaccess should appear now.
- Click on it and edit it.
Now Applying the Code in Your Htaccess
I told you earlier that it is risky. But do not panic. Before making changes to your htaccess, make sure you download it (have a backup of the htaccess file) in case anything goes wrong. How to download, long-press on the htaccess file, you should see download among the options that come out.
Now forcing download audio|mp3|pdf|mp4 file(s) to download automatically, you have to apply this code:
<FilesMatch "\.(?i:pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
The code above, works only if you want only one file type to force-download. As you can see, this only works for pdf files.. If you want it to work only for audio files, like Mp3, you just have to delete the pdf there and put Mp3.
On the other hand if you want to include multiple file type, use the code below:
<FilesMatch "\.(mov|mp3|jpg|pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>
You can see it combines, mov, Mp3, jpg and pdf together, at once. If you still want to add more file type, all you have to do is to include |file type within the bracket.
Note: The code should be applied just before the #End WordPress Tag in the htaccess.