如何配置Apache处理多个域与访问控制 - 允许 - 产地标头? [英] How to configure apache to handle multiple domains with Access-Control-Allow-Origin header?

查看:95
本文介绍了如何配置Apache处理多个域与访问控制 - 允许 - 产地标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置Apache以允许从多个XMLHtt prequests,但不是所有的领域。

I want to configure apache to allow XMLHttpRequests from multiple, but not all domains.

本作品:

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.com http://domain2.com"

不过,这只是拿起第一个域,二是不允许的。如何正确指定多个域?

But this only picks up first domain, the second is not allowed. How to properly specify multiple domains?

推荐答案

您可以在虚拟主机文件中使用SetEnvIf之后在你的.htaccess文件或(在目录组):

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\.com|mydomain2\.com)(:\d+)?$" AccessControlAllowOrigin=$0
   Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
</IfModule>

通过这个code,可允许从

With this code you can allow access from

  • 在mydomain.com和mydomain2.com
  • 在有或没有WWW。在前面
  • 在带或不带端口号
  • 在HTTP或HTTPS

您可以添加与分隔的多个域|或者您可以使用正则表达式来配​​置不同的子域或模式。

You can add multiple domains separated with | or you can use regexp to configure different subdomains or patterns.

这篇关于如何配置Apache处理多个域与访问控制 - 允许 - 产地标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆