I've got a directory of images that alternately be seen directly within the browser, and in other cases downloaded.

So, say I've got a file /gallery/woman_4254.digital.

I wish to make /download/woman_4254.digital trigger a download from the image instead of notice. /download is empty, all of the images have been in /gallery.

I'm able to map demands towards the download dir towards the other files effectively

<Directory /var/www/download>
    RewriteEngine on
    RewriteRule (.*)$ /gallery/$1
</Directory>

and that i already can pressure downloads within the gallery dir by setting

<Directory /var/www/gallery/>
    ForceType "image/jpg"
    Header set Content-Disposition "attachment"
</Directory>

so setting the headers isn't any problem. I do not really want /gallery to achieve the headers though, just demands for /gallery/* through /download/ which are rewritten.

But, I have to mix the 2, therefore the request is planned towards the file within the other dir And also the file is offered 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 have attempted altering an order from the rewrite and header sections with no success. It manages to lose the header once the request is rewritten towards the other directory.

Any suggestions regarding how to do that in Apache?

I recognize this may be completed with PHP too, and that's why I published it here versus. Server Fault. An answer using PHP could be welcome also.

A combined solution may be the following setup. First alter the directory entry:

<Directory /var/www/download>
    RewriteEngine on
    RewriteRule (.*)$ download.php?getfile=$1
</Directory>

The download.php should contain something similar to this (NOT Examined):

<?php

if ($_GET['getfile']){
  $file = '/var/www/gallery/' . $_GET['getfile'];
}

$save_as_name = basename($file);   
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header("Content-Type: application/octet-stream");
header("Content-Disposition: disposition-type=attachment; filename=\"$save_as_name\"");

readfile($file);
?>

This shouldd redirect all download demands to download.php which handles the demands and forces the saveas dialog to look.

Paul

Simple php solution:

download.php

header('Content-Type: image/jpeg');
header('Content-Disposition: attachment; filename='.$_GET['img']);
readfile('gallery/'.$_GET['img']);

.htaccess

<Directory /var/www/download>
    RewriteEngine on
    RewriteRule (.*)$ /download.php?img=$1
</Directory>

I simply did this, after tripping in your example -) I am confident you just need to improve your "Directory /var/www/download" section to some "Location /download" inside your latest example and you are OK.

Rationale is : "Directory" is applicable towards the resulting physical directory, Following the rewrites have happened, whereas "Location" is applicable towards the original URI, regardless of any rewrites have happened to obtain the physical file.

As mod_rewrite has become a hack that is applicable at various occasions, the result of the code is not so apparent.

Things I have during my workjing setup is :

<Location /contents/>
    Header set Content-Disposition "attachment"
</Location>
...
RewriteRule ^.*(/e-docs/.*)$   $1

So Web addresses like /contents/myimage.digital AND /contents/e-paperwork/myimage.digital both obtain the Content-Disposition header, despite the fact that /contents/e-paperwork/myimage.digital is really the /e-paperwork/myimage.digital file, because the rewrite states.

Staying away from PHP with this possess the additional benefit that you could serve these images and potentially huge video clips (as with my situation) having a lightweight static Apache server, not really a memory-hog PHP back-finish process.