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.jpgapache must add header Content-Disposition: attachment; filename="abc.jpg". How I can do it?

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.


转自: http://stackoverflow.com/questions/2272738/add-header-in-apache