简体   繁体   中英

Add header in apache

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" 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
</filesmatch>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM