在 Apache 中使用 Access-Control-Allow-Origin 标头处理多个域 [英] handle multiple domains with Access-Control-Allow-Origin header in Apache
问题描述
我想为跨域访问头配置apache.我已经按照论坛上的线程数量的建议尝试了多种组合.但它对我不起作用.
I want to configure apache for cross-domain access header. I have tried multiple combination as suggested on number of threads on the forum. But its not working for me.
方法,我试过:
1) 使用 Header set
在不同的行上指定域,如下所示:
1) Specify domain on different line as below with Header set
:
Header set Access-Control-Allow-Origin "example1.com"
Header set Access-Control-Allow-Origin "example2.com"
Header set Access-Control-Allow-Origin: "example3.com"
通过这种设置,它选择只选择最后一个而忽略其余的.
With this setup its picking only last one and ignore rest of all.
2) 使用 Header add
在不同的行上指定域,如下所示:
2) Specify domain on different line as below with Header add
:
Header add Access-Control-Allow-Origin "example1.com"
Header add Access-Control-Allow-Origin "example2.com"
Header add Access-Control-Allow-Origin: "example3.com"
这样它就在标题中显示了所有三个域,但在 Firefox 上没有选择字体.
With this its showing all three domains in header, but fonts are not getting picked up on Firefox.
3.) 尝试使用 SetEnvIf
,但它再次不起作用:
3.) Tried Using SetEnvIf
, but again its not working :
SetEnvIf Origin "http(s)?://(www\.)?(mydomain.com|mydomain2.com)$" AccessControlAllowOrigin=$0$1
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
终于使用*"了,但我不想使用它.
Finally working with "*", but I don't want to use this.
请帮忙解决这个问题.
推荐答案
对于 3 个域,在您的 .htaccess 中:
For 3 domains, in your .htaccess:
<IfModule mod_headers.c>
SetEnvIf Origin "http(s)?://(www\.)?(domain1.org|domain2.com|domain3.net)$" AccessControlAllowOrigin=$0$1
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Credentials true
</IfModule>
我已经试过了,它对我有用.如果不适合您,请告诉我.
I've tried this and it works for me. Let me know if it doesn't for you.
这篇关于在 Apache 中使用 Access-Control-Allow-Origin 标头处理多个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!