Question

I need to provide a button on a webpage that can optionally cause an image file to be downloaded and stored in the client's filesystem. I thought to use the approach from Force a file or image to download using .htaccess and wrote in my .htaccess, following the style of the Apache documentation,

<If "%{QUERY_STRING} =~ /download/">
Header set Content-Disposition attachment
</If>

I then provide a link like http://www.example.com/images/image123.jpeg?download=true, and that works fine with my production server. But it gives me an error 500 on the Mac development system on my desk, using either OS/X's apache or MAMP. Apache complains it does not understand the <If> statement, and serverFault tells me that is not understood by Apache 2.2. Both OS/X and MAMP provide Apache 2.2.

Is there a way I can rewrite the <if> statement to produce the same effect in Apache 2.2, please?

I can't just use

<a download="clientFileName.jpg" href="/images/image123.jpeg">Download</a> 

because it doesn't work in either IE11 or Safari.

Was it helpful?

Solution

You can set an env variable using rewrite rule first:

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)download=[^&]+ [NC]
RewriteRule ^ - [E=DL:1,L]

Then use this env variable in conditionally setting up your header:

Header set Content-Disposition attachment env=DL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top