本文介绍了有条件地设置 Apache 标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 apache 服务器,我想有条件地添加标头.

I'm working with an apache server, and I'd like to add headers conditionally.

如果 URI 匹配某个正则表达式,我想添加标题 Access-Control-Allow-Origin: *.这样做的好方法是什么?

If the URI matches a certain regex, I'd like to add the header Access-Control-Allow-Origin: *. What is a good way to do this?

到目前为止我尝试过的:

What I've tried so far:

  1. 我添加了由请求处理程序调用的代码,使用 apr_table_add(rq->headers_out, "Access-Control-Allow-Origin", "*").但是,每当还设置了标头 Content-Type: application/x-javascript 时,Apache 似乎都会在发送响应之前去除标头.这是错误的做法吗?为什么 Apache 会去掉标头?

  1. I added code called by the request handler, using 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. Is this the wrong way to do it? Why would Apache strip the header?

我听说过 mod_headers 建议.mod_headers 是否能够根据与请求 URI 匹配的正则表达式放置标头?

I've heard mod_headers suggested. Does mod_headers have the capability to place headers based on regex matching with the request URI?

推荐答案

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

但这仅在位于配置中时才有效.将其放入 .htaccess 无济于事.

but this works only if located in the configuration. Placing it into .htaccess won't help.

这篇关于有条件地设置 Apache 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:03