Is it possible to force loading with .htaccess and a specific GET request (Dropbox style)?

I wonder if it is possible with to .htaccess

force file uploads only if there is a specific GET variable, as Dropbox does.



the server just serves the file, and:


force the browser to download the file.

PS: Using PHP

with readfile()

is not a viable option in my case. Thank.


source to share

4 answers

The FilesMatch sample does not match the query string, only the filename.

However, you could use mod_rewrite module, variable %{QUERY_STRING}

and T flag :

RewriteCond %{QUERY_STRING} dl
RewriteRule .*\.mp4 - [T=application/octet-stream]


Alternatively, if you have Apache> 2.3 (I think), you can use the If directive , which is much cleaner:

<FilesMatch "filepattern.mp4">
   <If "%{QUERY_STRING} =~ /dl/">
      ForceType application/octet-stream
      Header set Content-Disposition attachment


The first solution has been updated.



I have a very similar requirement for my project. The question actually gave me the idea of ​​using a query parameter similar to how DropBox handles download links . I'm going to use the longer "dl = 1" parameter like DropBox, so it is unlikely to appear in a normal url. Using Apache "if directive" is a great idea, but unfortunately I'm on Apache 2.2 so it's not available. I considered using the Application Content-Type / stream octet, but then I saw this other stream that this idea didn't like. So finally I saw the BlogSpot articlewhich suggested setting an environment variable in the RewriteRule and referencing that same variable in the header command. Htaccess code works very well and solved the problem. Thanks to everyone on this thread and the BlogSpot guy for great suggestions. My sample code is below:

# Required Modules are:
# 1) mod_rewrite.c
# 2) mod_headers.c
RewriteEngine On
RewriteCond %{QUERY_STRING} ^dl=1
RewriteRule .* - [E=DOWNLOAD_FILE:1]
Header set Content-Disposition "attachment" env=DOWNLOAD_FILE




Alternatively, you can add a parameter to the tag and force the browser to open the download / save as prompt. It seems easier.

<a href="file.mp3" download >Download File</a>




Try the following:

<FilesMatch "\?dl$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment




All Articles