I have a directory of images that alternately be viewed directly in the browser, and other times downloaded.
So, say I have a file /gallery/gal_4254.jpg.
I want to make /download/gal_4254.jpg trigger a download of the image rather than view it. /download is empty, all the images are in /gallery.
I can map requests to the download dir to the other files successfully
<Directory /var/www/download>
RewriteEngine on
RewriteRule (.*)$ /gallery/$1
</Directory>
and I already can force downloads in the gallery dir by setting
<Directory /var/www/gallery/>
ForceType "image/jpg"
Header set Content-Disposition "attachment"
</Directory>
so setting the headers is no problem. I don't actually want /gallery to have the headers though, just requests for /gallery/* through /download/ that are rewritten.
But, I need to combine the two, so the request is mapped to the file in the other dir AND the file is given the attachment header.
#does not work - just views the image like when it is viewed directly
<Directory /var/www/download>
ForceType "image/jpg"
Header set Content-Disposition "attachment"
RewriteEngine on
RewriteRule (.*)$ /gallery/$1
</Directory>
I've tried changing the order of the rewrite and header sections to no avail. I think it loses the header when the request is rewritten to the other directory.
Any suggestions on how to do this in Apache?
I realize this could be done with PHP as well, which is why I posted it here vs. Server Fault. A solution using PHP would be welcome also.