Set Apache headers conditionally

I am working with apache server and I would like to add headers conditionally.

If the URI matches a specific regular expression, I would like to add an Access-Control-Allow-Origin: * header. What is a good way to do this?

What I have tried so far:

  • I added the code called by the request handler using apr_table_add(rq->headers_out, "Access-Control-Allow-Origin", "*") . But it seems that Apache is breaking the header before sending a response whenever the Content-Type: application/x-javascript header is also set. Is this the wrong way to do this? Why did Apache split the header?

  • I heard mod_headers suggestions. Does mod_headers have the ability to place headers based on regular expression matching with the request URI?

+7
cors apache mod-headers
source share
2 answers
 SetEnvIf Request_URI somepartofurl SIGN Header always add "Access-Control-Allow-Origin" "*" env=SIGN 

but this only works if it is in the configuration. Placing it in .htaccess will not help.

+11
source share

This should also work (mod_rewrite required):

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

where ^/en/foo.*$ regular expression that maps to the request URL

+2
source share

All Articles