By default, when a file is requested through a URL, the contents will appear in the web browser. The following guide will show you how you can force a file to be downloaded when visited through its URL.
In this example, we have a text file on our website. This file will be called test.txt.
- When this file is requested through a URL, the contents are displayed in the browser.
- To change this, navigate to your website's root directory. root directory. root directory.
- Insert the following code into the .htaccess file:
AddType application/octet-stream txt
- Now, if we re-attempt to request the test.txt file through a browser, the file should be automatically downloaded.
You may need to clear your caches in order for the change to applied.
- To expand force downloads to include other extensions, the following code can be added to the .htaccess file:
The code below will force .mov, .mp3, .jpg, and .pdf files to be downloaded.
<FilesMatch "\.(mov|mp3|jpg|pdf)$"> ForceType application/octet-stream Header set Content-Disposition attachment </FilesMatch>