Reputation: 769
I want to configure Apache to allow XMLHttpRequests from multiple, but not all domains.
This works:
Header set Access-Control-Allow-Origin "*"
But it's unsafe, I want to allow domains specified by me, so after a bit of googling I got to this:
Header set Access-Control-Allow-Origin "http://domain1.example http://domain2.example"
But this only picks up first domain, the second is not allowed. How to properly specify multiple domains?
Upvotes: 18
Views: 26465
Reputation: 493
you can use SetEnvIf
in your .htaccess
file or in in vhost file (inside "Directory" group):
<IfModule mod_headers.c>
SetEnvIfNoCase Origin "https?://(www\.)?(mydomain\.example|mydomain2\.example)(:\d+)?$" AccessControlAllowOrigin=$0
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>
With this code you can allow access from
mydomain.example
and mydomain2.example
You can add multiple domains separated with | or you can use regexp to configure different subdomains or patterns.
Upvotes: 45