我在尝试加载超赞字体时面临访问控制来源问题。这是 Chrome 浏览器控制台中记录的错误消息。
Access to Font at 'https://example.com/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0' from origin 'https://example2.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example2.com' is therefore not allowed access.
我已经更新了 apache2 服务器 httpd.conf 并启用了 header 。
<IfModule mod_mime.c>
AddType application/font-woff2 .woff2
</IfModule>
<FilesMatch "\.(ttf|ttc|otf|eot|woff2|woff|font.css|css|js)$">
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
</FilesMatch>
我也尝试过对 .htaccess 文件进行相同的设置,但结果是一样的。这有点奇怪,因为我只遇到 woff2 文件扩展名的问题。
最佳答案
如何使用服务器脚本而不是物理字体文件
<style type="text/css">
@font-face {
font-family: 'OpenSans';
src: url('path/to/fonts.php') format('woff2');
}
html, body{
font: normal 16px OpenSans, sans-serif;
}
</style>
如何解决 PHP 的跨域@font-face 问题
<?php
// fonts.php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/font-woff2");
echo @file_get_contents("path/fontawesome-webfont.woff2");
?>
关于Apache 访问控制允许 woff2 文件类型的来源问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43871898/