如何通过htaccess允许基于域名的访问控制源

在我的 files 目录中,我有一个 custom-fonts.css 文件,该文件也引用了位于 files 文件夹中的字体文件。

我们的目标是让 angular 应用使用通过 CMS 加载的那些字体。但是,即使我在 services.yml 中允许该来源,在从我的本地主机引用字体时也会出现 cors 错误。

services.yml

  cors.config:
    enabled: true
    # Specify allowed headers,like 'x-allowed-header'.
    allowedHeaders: ['x-csrf-token','authorization','content-type','accept','origin','x-requested-with','access-control-allow-origin','x-allowed-header','*']
    # Specify allowed request methods,specify ['*'] to allow all possible ones.
    allowedMethods: ['*']
    # Configure requests allowed from specific origins.
    allowedOrigins: ['http://localhost/','http://localhost:4000']
    # Sets the access-Control-Expose-Headers header.
    exposedHeaders: false
    # Sets the access-Control-Max-Age header.
    maxAge: false
    # Sets the access-Control-Allow-Credentials header.
    supportsCredentials: false

然后我注意到这是一个已知问题,因为 acquia 中的清漆。链接here

继续前进,我决定允许通过 htaccess 来源作为解决方案,并在 SO 上的某处提供以下内容:

<IfModule mod_headers.c>
    SetEnvIf Origin "^http(s)?://(.+\.)?(localhost|localhost:4000localhost:4200|domain1.com:5100|domain2.com|domain3.com:7080/home/|domain4.com:7080/home/|domain5.com:7080/home/)$" origin_is=$0
    Header always set access-control-allow-origin %{origin_is}e env=origin_is
</IfModule>

经过几次测试,我发现这个代码段根本不起作用。

有没有一种方法可以使用 if 语句来检查域名,然后相应地设置标题?

更新:

目前,我只有本地主机

SetEnvIf Origin "^http(s)?://(.+\.)?(localhost|localhost:3200|localhost:4200)$" origin_is=$0

然而,刷新页面时,我仍然收到 CORS 错误,当我检查网络选项卡时,似乎没有设置访问控制。

我使用 this post 作为参考。

ergasfgsafg 回答:如何通过htaccess允许基于域名的访问控制源

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/13713.html

大家都在问