apache 在apache中添加标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2272738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:47:24  来源:igfitidea点击:

Add header in apache

apacheheader

提问by Leonid

I have apache and many images on this server. E. g.: http://test.com/images/abc.jpgI 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?

我在这台服务器上有 apache 和许多图像。例如:http://test.com/images/abc.jpg我想有可能这样做:如果用户像这样访问 url http://test.com/images/download/abc.jpgapache 必须添加 header Content-Disposition: attachment; filename="abc.jpg"。我该怎么做?

回答by Jeremy Stein

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

您想使用 mod_rewrite(伪造“下载”目录)和 mod_headers(添加 Content-Disposition 标头)的组合。

Create a .htaccess file in the images directory:

在图像目录中创建一个 .htaccess 文件:

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

每当设置“REDIRECT_force_download”环境变量时,这将设置适当的标头。

Create a download directory and add this .htaccess file:

创建一个下载目录并添加这个 .htaccess 文件:

RewriteEngine On
RewriteRule (.*) ../ [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.

这会将任何图像请求重定向到父(图像)目录,同时添加将从上面触发 Header 命令的“REDIRECT_force_download”环境变量。