htaccess 多个 headers 返回 CORS 不匹配

htaccess multiple headers returning CORS mismatch

我关注了https://developer.mozilla.org/en-US/docs/Learn/Server-side/Apache_Configuration_htaccess#cross-origin_images and these Whosebug answers Access-Control-Allow-Origin Multiple Origin Domains?

尝试让它很好地工作。但它在某些来源和某些来源上造成了不匹配,它按预期工作。

htaccess 更新

<IfModule mod_rewrite.c>
<IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      # Allowing fonts for specific origins on mtn domains and local testing
      <FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
        SetEnvIf Origin "^http(s)?://(.+\.)?(fiddle\.jshell\.net|lab4\.onlinecms\.mtn\.co\.za|mtndecoupled\.lndo\.site:444|localhost:4200)$" AccessControlAllowOrigin=[=11=]
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
        Header set Access-Control-Allow-Credentials true
      </FilesMatch>
    </IfModule>
  </IfModule>
</IfModule>

CORS错误截图:

mtndecoupled.lndo.site:444https://share.getcloudapp.com/geuA8QxY
lab4.onlinecms.mtn.co.zahttps://share.getcloudapp.com/Wnu0z94G
localhost:4200 https://share.getcloudapp.com/P8uGvzgx
jsfiddle https://jsfiddle.net/ft92z8c3/1

编辑

浏览器控制台中的错误消息:

CORS header 'Access-Control-Allow-Origin' does not match 'mtndecoupled.lndo.site:444'

不确定是模块检查还是设置环境正则表达式。我希望有人可以为此提供更多信息以及我如何解决它。

感谢任何反馈。

好的,成功解决了这个问题。希望这可以帮助遇到此问题的其他人。

我们正在使用 Varnish 缓存并意识到它正在缓存第一个域命中。所以我们将 htaccess 更新为:

<IfModule mod_rewrite.c>
<IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      # Disable caching
      Header set Cache-Control "no-cache, no-store, must-revalidate"
      Header set Pragma "no-cache"
      Header set Expires 0
      # Allowing fonts for specific origins on mtn domains and local testing
      <FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
        <IfModule mod_expires.c>
            ExpiresActive Off
        </IfModule>
        <IfModule mod_headers.c>
              FileETag None
              Header unset ETag
              Header unset Pragma
              Header unset Cache-Control
              Header unset Last-Modified
              Header set Pragma "no-cache"
              Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
              Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
        SetEnvIf Origin "^http(s)?://(.+\.)?(fiddle\.jshell\.net|lab4\.onlinecms\.mtn\.co\.za|mtndecoupled\.lndo\.site:444|localhost:4200)$" AccessControlAllowOrigin=[=10=]
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
        Header set Access-Control-Allow-Credentials true
        </IfModule>
      </FilesMatch>
    </IfModule>
  </IfModule>
</IfModule>