Apache 访问控制允许 woff2 文件类型的来源问题

标签 apache .htaccess font-face font-awesome

我在尝试加载超赞字体时面临访问控制来源问题。这是 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");
?>

Reference

关于Apache 访问控制允许 woff2 文件类型的来源问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43871898/

相关文章:

html - 有没有办法检查字体系列是否正在渲染,而不是简单地观察它?

tomcat - HTTPS (SSL) IE9 - FontAwesome 无法正确显示,仅显示正方形。 | Apache 汤姆猫

css - @font-face EOT 不通过 HTTPS 加载

php - 在 mysql 中使用 REGEXP 时出错

.htaccess - 直接链接到我的 URL 的问题 (https ://)

php - 试图找出 htaccess 问题

apache - 如何使用 `RewriteRule ^(.*)$ ?id=$1 [L,QSA]` 通过 .htaccess 获取文件目录?

ruby-on-rails - 在 Centos 上安装 Redmine

php - Google Chrome 强制下载 PHP 脚本的源代码

html - CSS 在 apache2 服务器上的 Flask 应用程序中不起作用