Set Apache headers conditionally

巧了我就是萌 提交于 2019-12-03 17:58:02

问题


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

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

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


回答1:


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.




回答2:


This should also work (mod_rewrite is required):

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



来源:https://stackoverflow.com/questions/21270902/set-apache-headers-conditionally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!