如何在 Apache 中向 302 重定向添加其他标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/690521/
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
How to add additional headers to 302 redirects in Apache?
提问by deadprogrammer
I have a redirect in Apache config like
我在 Apache 配置中有一个重定向,例如
Redirect temp /foo.xml http://www.baz.com/foo.xml
重定向 temp /foo.xml http://www.baz.com/foo.xml
I am trying to add an Expire and m-cache headers for a CDN to this 302. This would be trivial in php, but I need to do this in Apache config files.
我正在尝试将 CDN 的 Expire 和 m-cache 标头添加到此 302。这在 php 中是微不足道的,但我需要在 Apache 配置文件中执行此操作。
Normally this is done like this:
通常这是这样做的:
ExpiresActive On ExpiresDefault "access plus 10 minutes"
ExpiresActive On ExpiresDefault“访问加10分钟”
but this only seems to not work for 302 redirects. Any suggestions?
但这似乎只对 302 重定向不起作用。有什么建议?
采纳答案by Richard
Check out the mod_headers module for Apache.
查看 Apache 的 mod_headers 模块。
Perhaps something like:
也许是这样的:
<Location /foo.xml>
Redirect temp /foo.xml http://www.baz.com/foo.xml
Header always set ExpiresActive On
Header always set ExpiresDefault "access plus 10 minutes"
</Location>
I have edited this answer (since it was accepted), adding the alwayskeyword, to reflect what Fix correctly pointed out below.
我已经编辑了这个答案(因为它被接受了),添加了always关键字,以反映 Fix 在下面正确指出的内容。
回答by Fix
<Location /foo.xml>
Redirect temp /foo.xml http://www.baz.com/foo.xml
Header always set ExpiresActive On
Header always set ExpiresDefault "access plus 10 minutes"
</Location>
to get it working even with HTTP 302 responses (actually, with any HTTP response) ; without the keyword "always", the directive "Header set" works only with success responses, i.e. HTTP 2xx responses.
即使使用 HTTP 302 响应(实际上,使用任何 HTTP 响应)也能使其正常工作;如果没有关键字“ always”,则指令“Header set”仅适用于成功响应,即 HTTP 2xx 响应。
回答by Mike Godin
Please note an odd bug in Apache 2.2 (observed in Apache 2.2.15) that makes this difficult if you are using env=HTTPSto control when the Header is being set. For some reason env=HTTPSfails to fire during redirects, even if RewriteCond %{HTTPS}on is used to control the redirect. So in my configuration that enables HTTP Strict Transport Security (HSTS), I use use a RewriteRule to create an environment variable called X_HTTPSthat has the same value as the HTTPS, but which is not set to null when env=X_HTTPSis evaluated:
请注意 Apache 2.2 中的一个奇怪的错误(在 Apache 2.2.15 中观察到),如果您env=HTTPS在设置 Header 时使用它来控制,这会使这变得困难。出于某种原因env=HTTPS,在重定向期间无法触发,即使RewriteCond %{HTTPS}on 用于控制重定向。因此,在启用 HTTP 严格传输安全性 (HSTS) 的配置中,我使用 RewriteRule 创建一个名为的环境变量X_HTTPS,该变量具有与 相同的值HTTPS,但在env=X_HTTPS评估时未设置为 null :
SetEnv HSTS "max-age=31536000; includeSubDomains; preload"
# For some reason in Apache 2.2, HTTPS env variable is not available during redirects
RewriteCond %{HTTPS} on
RewriteRule ^.*$ - [ENV=X_HTTPS:%{HTTPS}]
# Set HSTS Header if the page is delivered via SSL
Header always set Strict-Transport-Security %{HSTS}e env=X_HTTPS
# Redirect SSL-only non-www page to www and quit
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=303,L]
# Redirect ANY non-SSL page to SSL
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=303,L]

