Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access-Control-Allow-Origin Multiple Origin Domains?

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?

like image 999
Thomas J Bradley Avatar asked Oct 31 '09 03:10

Thomas J Bradley


People also ask

How do I allow multiple domains in Access-Control allow origin?

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 the Access-Control-Allow-Origin header in the response.

Does the Access-Control allow Origin header contains multiple values?

The 'Access-Control-Allow-Origin' header contains multiple values, but only one is allowed.

How do I fix Access-Control allow Origin Cors origins?

Since the header is currently set to allow access only from https://yoursite.com , the browser will block access to the resource and you will see an error in your console. Now, to fix this, change the headers to this: res. setHeader("Access-Control-Allow-Origin", "*");

Is Access-Control allow Origin * Insecure?

The 'Access-Control-Allow-Origin' header is insecure when set to '*' or null, as it allows any domain to perform cross-domain requests and read responses.


2 Answers

Another solution I'm using in PHP:

$http_origin = $_SERVER['HTTP_ORIGIN'];  if ($http_origin == "http://www.domain1.com" || $http_origin == "http://www.domain2.com" || $http_origin == "http://www.domain3.com") {       header("Access-Control-Allow-Origin: $http_origin"); } 
like image 35
Nikolay Ivanov Avatar answered Oct 20 '22 18:10

Nikolay Ivanov


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 the Access-Control-Allow-Origin header in the response.

With .htaccess you can do it like this:

# ---------------------------------------------------------------------- # Allow loading of external fonts # ---------------------------------------------------------------------- <FilesMatch "\.(ttf|otf|eot|woff|woff2)$">     <IfModule mod_headers.c>         SetEnvIf Origin "http(s)?://(www\.)?(google.com|staging.google.com|development.google.com|otherdomain.example|dev02.otherdomain.example)$" AccessControlAllowOrigin=$0         Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin         Header merge Vary Origin     </IfModule> </FilesMatch> 
like image 124
yesthatguy Avatar answered Oct 20 '22 18:10

yesthatguy