Access-Control-Allow-Origin FilesMatch 不适用于 EOT 以外的类型

too*_*les 5 regex .htaccess cross-domain http-headers

我正在从无 Cookie 的域为我的网站提供资源。由于跨站点原因,此功能在 Chrome 和 Firefox 中被阻止

\n\n
Redirect at origin \'http://static.domain.com\' has been blocked from loading by Cross-Origin Resource Sharing policy: No \'Access-Control-Allow-Origin\' header is present on the requested resource. Origin \'http://www.domain.com\' is therefore not allowed access.\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,我将以下内容添加到 htaccess 文件中以启用 CORS

\n\n
<IfModule mod_setenvif.c>\n  <IfModule mod_headers.c>\n    # mod_headers, y u no match by Content-Type?!\n    <FilesMatch "\\.(gif|png|jpe?g|svg|svgz|ico|webp)$">\n      SetEnvIf Origin ":" IS_CORS\n      Header set Access-Control-Allow-Origin "*" env=IS_CORS\n    </FilesMatch>\n  </IfModule>\n</IfModule>\n\n<IfModule mod_headers.c>\n  <FilesMatch "\\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js|svg)$\xe2\x80\x9d>\n    Header set Access-Control-Allow-Origin \xe2\x80\x9c*\xe2\x80\x9d\n  </FilesMatch>\n</IfModule>\n
Run Code Online (Sandbox Code Playgroud)\n\n

由于某种原因,这不能正常工作 - eot 文件返回正确的标头,但 ttf、woff 和 svg 则不会。我有点困惑为什么!我在语法上犯了一些错误吗?

\n\n

EOT标头

\n\n
Access-Control-Allow-Origin *\nDate    Sun, 25 Oct 2015 19:41:30 GMT\nLast-Modified   Sat, 24 Oct 2015 10:22:45 GMT\nServer  cloudflare-nginx\nCF-RAY  23b066397718352a-LHR\nContent-Type    application/vnd.ms-fontobject\n
Run Code Online (Sandbox Code Playgroud)\n\n

TTF 标头

\n\n
Date    Sun, 25 Oct 2015 21:57:58 GMT\nLast-Modified   Sat, 24 Oct 2015 10:22:44 GMT\nServer  cloudflare-nginx\nContent-Type    application/x-font-ttf\nCache-Control   max-age=31104000\nAccept-Ranges   bytes\nCF-RAY  23b12e202e1735ea-LHR\nContent-Length  199248\nExpires Wed, 19 Oct 2016 21:57:56 GMT\n
Run Code Online (Sandbox Code Playgroud)\n

Bar*_*ard 2

我注意到 ttf 被缓存,而 eot 没有。当您使用 cloudfare 时,它​​很可能遵循这些缓存指令,因此提供缓存版本。

请参阅此处了解更多信息以及可能如何解决:https://support.cloudflare.com/hc/en-us/articles/203063414-Why-can-tI-see-my-CORS-headers-