简体   繁体   English

有条件地设置Apache标头

[英]Set Apache headers conditionally

I'm working with an apache server, and I'd like to add headers conditionally. 我正在使用apache服务器,我想有条件地添加标头。

If the URI matches a certain regex, I'd like to add the header Access-Control-Allow-Origin: * . 如果URI匹配某个正则表达式,我想添加标题Access-Control-Allow-Origin: * What is a good way to do this? 有什么好办法呢?

What I've tried so far: 到目前为止我尝试过的:

  1. I added code called by the request handler, using apr_table_add(rq->headers_out, "Access-Control-Allow-Origin", "*") . 我添加了请求处理程序调用的代码,使用apr_table_add(rq->headers_out, "Access-Control-Allow-Origin", "*") But it seems like Apache strips the header before sending the response whenever the header Content-Type: application/x-javascript is also set. 但是,每当标题Content-Type: application/x-javascript也被设置时,似乎Apache在发送响应之前剥离了标头。 Is this the wrong way to do it? 这是错误的方法吗? Why would Apache strip the header? 为什么Apache会删除标题?

  2. I've heard mod_headers suggested. 我听说mod_headers建议了。 Does mod_headers have the capability to place headers based on regex matching with the request URI? mod_headers是否能够根据与请求URI匹配的正则表达式放置标头?

SetEnvIf Request_URI somepartofurl SIGN
Header always add "Access-Control-Allow-Origin" "*" env=SIGN

but this works only if located in the configuration. 但这仅适用于配置中的情况。 Placing it into .htaccess won't help. 将它放入.htaccess将无济于事。

This should also work (mod_rewrite is required): 这也应该有效(需要mod_rewrite):

RewriteRule ^/en/foo.*$ - [ENV=SET_ACAO:true]
Header set "Access-Control-Allow-Origin" "*" env=SET_ACAO

where ^/en/foo.*$ a regex which is matched against request URL 其中^/en/foo.*$一个与请求URL匹配的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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