I have apache and many images on this server. E. g.: http://test.com/images/abc.jpg I want to have possibility do this: if user go to url like this http://test.com/images/download/abc.jpg apache must add header Content-Disposition: attachment; filename="abc.jpg". How I can do it?


What Apache product do you have? Having Apache means a lot of things, such as Tomcat, James, Geronimo, etc.


Written by Leonid

Accepted Answer

You want to use a combination of mod_rewrite (to fake the "download" directory) and mod_headers (to add the Content-Disposition header).

Create a .htaccess file in the images directory:

<filesmatch ".*">
Header set Content-Disposition attachment env=REDIRECT_force_download

This will set the appropriate header whenever the "REDIRECT_force_download" environment variable is set.

Create a download directory and add this .htaccess file:

RewriteEngine On
RewriteRule (.*) ../$1 [L,NC,QSA,E=force_download:1]

This will redirect any image requests to the parent (images) directory while adding the "REDIRECT_force_download" environment variable that will trigger the Header command from above.

Written by Jeremy Stein
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki