跨域字体问题

发布于 2024-12-18 02:17:32 字数 1442 浏览 0 评论 0原文

请在发表评论之前阅读所有内容。

我目前正在开发一个托管在 Amazon Web Services (AWS) 上的大型网站。这使我们能够在网站可能承受较大流量负载的情况下使用可扩展性功能。

最初,我们首先将网站的代码分离为 HTML/PHP/Java 等的混合体,并在单独的服务器上拥有静态资产。当我第一次尝试在此设置中使用 font-face 时,我发现 Firefox 和 IE 无法加载字体,并且很快发现这是一个跨域问题。对此有多种解决方案,其中包括修改标头以允许访问字体文件。然而,我们使用的存储系统不允许这种类型的标头修改。

为了看看我是否能让这些字体在所有浏览器上工作,我将它们以及调用它们的 CSS 文件移动到与网站相同的域。然而,它们似乎仍然无法在 Firefox 或 IE 中加载。如果我复制代码并在文档中本地运行它,那么在所有浏览器中一切似乎都很好(因此文件不会损坏)。 Firefox 确实找到了这些文件,因为我可以看到它们是通过 FireBug 加载的。

我已经检查了所有 URL,以确保它们正确并解析它们应该解析的位置。

这是我在笑脸黑客中使用的字体 CSS:

@font-face {
    font-family : "AllerRegular";
    src : url('aller_rg-webfont.eot');
    src : local('☺'),
          url('aller_rg-webfont.woff') format('woff'), 
          url('aller_rg-webfont.ttf') format('truetype'), 
          url('aller_rg-webfont.svg#webfontooYDBZYS') format('svg');
    font-weight : normal;
    font-style : normal;
}

CSS 文件与字体位于同一目录中。

我还在 .htaccess 文件中设置了 MIME 类型,该文件与字体位于同一文件夹中。

AddType application/vnd.ms-fontobject .eot
AddType font/truetype .ttf
AddType font/opentype .otf
AddType font/opentype .woff
AddType image/svg+xml .svg .svgz
AddEncoding gzip .svgz
<FilesMatch "\.(ttf|otf|eot|woff|svg)$">
        <IfModule mod_headers.c>
                Header set Access-Control-Allow-Origin "*"
        </IfModule>
</FilesMatch>

如果您有任何想法,请提出建议。
我已经在网上搜索了几天,但所有解决方案都让我失败。

please read all of this before commenting.

I'm currently working on a large website which is hosted on Amazon Web Services (AWS). This allows us to use scalability features in situations where the website might take a big traffic load.

Originally we started by separating out the code of the website a mix of HTML/PHP/Java etc and have static assets on a separate server. When I first tried using font-face in this setup I found that Firefox and IE would not load the font, and quickly discovered it was a cross-domain issue. There are a variety of solutions to this, which include modifying headers to allow access to the font files. However, the storage system we're using doesn't allow for this type of header modification.

In a bid to see if I could just get the fonts to work across all browsers I moved them and the CSS file that calls them to the same domain as the website. However they still don't seem to load up in Firefox or IE. If I copy the code and run it locally in my documents everything seems fine in all browsers (so the files cannot be corrupt). Firefox is definitely finding the files as I can see them being loaded via FireBug.

I've checked all URLs to make sure they're correct and resolve where they should.

Here's the font-face CSS I'm using with the smiley hack:

@font-face {
    font-family : "AllerRegular";
    src : url('aller_rg-webfont.eot');
    src : local('☺'),
          url('aller_rg-webfont.woff') format('woff'), 
          url('aller_rg-webfont.ttf') format('truetype'), 
          url('aller_rg-webfont.svg#webfontooYDBZYS') format('svg');
    font-weight : normal;
    font-style : normal;
}

The CSS file sits in the same directory as the fonts.

I also have the MIME types set in a .htaccess file which sits in the same folder as the fonts.

AddType application/vnd.ms-fontobject .eot
AddType font/truetype .ttf
AddType font/opentype .otf
AddType font/opentype .woff
AddType image/svg+xml .svg .svgz
AddEncoding gzip .svgz
<FilesMatch "\.(ttf|otf|eot|woff|svg)$">
        <IfModule mod_headers.c>
                Header set Access-Control-Allow-Origin "*"
        </IfModule>
</FilesMatch>

If you have any ideas please suggest.
I've been searching the web for a few days now but all solutions have failed me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

咿呀咿呀哟 2024-12-25 02:17:32

目前,从 AWS 提供的网络字体可能无法在 Firefox 和 IE 9+ 中运行,因为 AWS 不支持 Access-Control-Origin-Header。请参阅 AWS 论坛上的此帖子 。似乎是很多人的问题。

2012 年 7 月 17 日更新:

作为 AWS 的替代方案,Google 的云服务支持跨域文件服务。我刚刚设置了一个存储桶来提供网络字体,它似乎正在工作。请参阅此博客帖子文档了解更多信息。

Currently, serving webfonts from AWS likely won't work in Firefox and IE 9+ because AWS doesn't support the Access-Control-Origin-Header. See this post on the AWS forums. Seems to be a problem for a lot of people.

Update 7/17/12:

As an alternative to AWS, Google's cloud services support cross-origin file serving. I just set up a bucket to serve webfonts, and it seems to be working. See this blog post and the documentation for more info.

感情废物 2024-12-25 02:17:32

尝试这样做:

在您的服务器上,您需要添加:

Access-Control-Allow-Origin

到字体文件的标头,例如,如果您使用 Apache,您可以将其添加到 .htaccess:

<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

我的问题用此方法解决了。

Try do this:

On your server you will need to add:

Access-Control-Allow-Origin

To the header of the font files, so for example if you are using Apache you can add this to the .htaccess:

<FilesMatch "\.(ttf|otf|eot|woff)$">
  <IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
  </IfModule>
</FilesMatch>

My problem solved with this method.

七七 2024-12-25 02:17:32

通过添加以下 CORS 配置,可以在没有 Cloudfront 的情况下使用 S3。

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Content-*</AllowedHeader>
    <AllowedHeader>Host</AllowedHeader>
  </CORSRule>
</CORSConfiguration>

http://docs.aws.amazon.com/AmazonS3/latest/dev /cors.html

http://html5hacks.com/blog/2012/11/18/configure-amazon-s3-for-cross-origin-resourse-sharing-to-host-a-web-font/

<一个href="http://blog.blenderbox.com/2012/10/12/serving-font-face-fonts-to-firefox-from-an-s3-bucket/" rel="nofollow">http:// blog.blenderbox.com/2012/10/12/serving-font-face-fonts-to-firefox-from-an-s3-bucket/

This is possible using S3 without Cloudfront by adding the following CORS configuration.

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Content-*</AllowedHeader>
    <AllowedHeader>Host</AllowedHeader>
  </CORSRule>
</CORSConfiguration>

http://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html

http://html5hacks.com/blog/2012/11/18/configure-amazon-s3-for-cross-origin-resourse-sharing-to-host-a-web-font/

http://blog.blenderbox.com/2012/10/12/serving-font-face-fonts-to-firefox-from-an-s3-bucket/

随遇而安 2024-12-25 02:17:32

您可能很需要在新的托管环境上添加对 MIME 类型的支持。

看一下:

http://www.jbarker.com/blog/ 2011/web-fonts-mime-types

You may well need to add support for the MIME types on the new hosting environment.

Take a look at:

http://www.jbarker.com/blog/2011/web-fonts-mime-types

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文