Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin
header?
I'm aware of the *
, but it is too open. I really want to allow just a couple domains.
As an example, something like this:
Access-Control-Allow-Origin: http://domain1.example, http://domain2.example
I have tried the above code but it does not seem to work in Firefox.
Is it possible to specify multiple domains or am I stuck with just one?
Sounds like the recommended way to do it is to have your server read the Origin header from the client, compare that to the list of domains you would like to allow, and if it matches, echo the value of the
Origin
header back to the client as theAccess-Control-Allow-Origin
header in the response.With
.htaccess
you can do it like this:For Nginx users to allow CORS for multiple domains. I like the @marshall's example although his anwers only matches one domain. To match a list of domain and subdomain this regex make it ease to work with fonts:
This will only echo "Access-Control-Allow-Origin" headers that matches with the given list of domains.
To facilitate multiple domain access for an ASMX service, I created this function in the global.asax file:
This allows for CORS handling of
OPTIONS
verb also.Here's an expanded option for apache that includes some of the latest and planned font definitions:
I had the same problem with woff-fonts, multiple subdomains had to have access. To allow subdomains I added something like this to my httpd.conf:
For multiple domains you could just change the regex in
SetEnvIf
.For IIS 7.5+ with URL Rewrite 2.0 module installed please see this SO answer