How to rewrite and set headers at the same time in Apache

I have a catalog of images that are alternately viewed directly in the browser and downloaded at another time.

So let's say I have the file / gallery / gal _4254.jpg.

I want to make / download / gal _4254.jpg start loading the image, and not view it. / Download is empty, all images are in / gallery.

I can match download directory requests with other files successfully

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

and I can already force upload files to the gallery gallery by setting

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

therefore setting the headers is not a problem. I don’t want the gallery to have headers at all, it just asks for / gallery / * through / download /, which are being overwritten.

, , .

#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>

. , , .

, Apache?

, PHP, " ". PHP.

+3
3

php:

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>
+2

. :

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

download.php - (NOT TESTED):

<?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);
?>

download.php, , , .

+2

, ;-) , "Directory/var/www/download" "Location/download" , .

: "" , , , "" URI, , - , .

mod_rewrite - , , .

, :

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

, URL-, /contents/myimage.jpg AND/contents/e-docs/myimage.jpg, Content-Disposition, /contents/e -docs/myimage.jpg /e -docs/myimage.jpg, .

PHP : ( ) Apache, back-end- PHP-hog.

+1

Source: https://habr.com/ru/post/1735080/


All Articles