How to Force a File to Download Using Apache .htaccess


how to force a file to download using apache .htaccess

Typically, when users access a file (pdf, image, videos, etc) on your website, it will open for them in the browser. However, this means more resources are consumed on your server while trying to serve the response. So unless your users stream videos/songs on your site, it is advisable to force them to download it. Let us take a look at how to force a file to download using Apache .htaccess.

 

How to Force a File to Download Using Apache .htaccess

Here are the steps to force a file to download using Apache .htaccess. We will look at how to do it with apache confif file as well as.htaccess file. However, before proceeding, please ensure you have enabled .htaccess file for your Apache web server. Here are the steps to do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html)

If You Have Access to Config File

 

1. Open config file

Open Apache config file in a text editor.

You will find it at one of the following locations, depending on your Linux distribution and type of installation.

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

 


$ sudo vim /etc/apache2/httpd.conf

 

2. Force File Download

Let’s say you want to force file download for all PDF files in /uploads/ folder, then add the following line to your Apache config file.


<Location "/uploads/">
 <Files *.pdf>
  ForceType application/octet-stream
  Header set Content-Disposition attachment
 </Files>
</Location>

TheĀ Location block specifies the folder for which you want to force downloads. The Files block specifies the file names for which you want to apply the rules. If you want to enforce download for all files, replace *.pdf with *.* above.

If you want to force download for multiple file types, you can use the ‘|’ operator to combine them as shown below.


<Location "/uploads/">
 <Files "\.(mov|mp3|jpg|pdf|mp4|avi|wmv)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
 </Files>
</Location>

 

Please Note:

If you want to force download across entire website, don’t specify the Location block

 

Alternatively, you can also do the same thing using FilesMatch directive, as shown below


<FilesMatch "\.(mov|mp3|jpg|pdf|mp4|avi|wmv)$">
 ForceType application/octet-stream
 Header set Content-Disposition attachment
</FilesMatch>

 

 

If You Don’t Have Access to Config File

 

1. Open .htaccess file

Open .htaccess file in a text editor. You will find it at /var/www/html


$ sudo vim /var/www/html/.htaccess

 

2. Force File Download

Let’s say you want to force file download for all PDF files in /uploads/ folder, then add the following line to your .htaccess file.


<Files *.pdf>
 ForceType application/octet-stream
 Header set Content-Disposition attachment
</Files>

TheĀ Location block is not supported in .htaccess. So we directly use the Files directive. The Files block specifies the file names for which you want to apply the rules. If you want to enforce download for all files, replace *.pdf with *.* above.

If you want to force download for multiple file types, you can use the ‘|’ operator to combine them as shown below.


<Files "\.(mov|mp3|jpg|pdf|mp4|avi|wmv)$">
 ForceType application/octet-stream
 Header set Content-Disposition attachment
</Files>

 

Please Note:

The above code will force download for the specified file types, across your entire website. If you want to force download only for a specific directory (e.g /uploads), then create a blank .htaccess file in that folder and add the above code in it.

 

Alternatively, you can also do the same thing using FilesMatch directive, as shown below


<FilesMatch "\.(mov|mp3|jpg|pdf|mp4|avi|wmv)$">
 ForceType application/octet-stream
 Header set Content-Disposition attachment
</FilesMatch>

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!