WOFF 字体应该用作什么 mime 类型?
我提供 truetype (ttf) 字体font/truetype
和 opentype (otf) as font/opentype
,但我找不到 WOFF 字体的正确格式。
我已经尝试过font/woff
,font/webopen
和font/webopentype
,但 Chrome 仍然抱怨:
“资源解释为字体,但使用 MIME 类型应用程序/八位字节流传输。”
有人知道吗?
WOFF 字体应该用作什么 mime 类型?
我提供 truetype (ttf) 字体font/truetype
和 opentype (otf) as font/opentype
,但我找不到 WOFF 字体的正确格式。
我已经尝试过font/woff
,font/webopen
和font/webopentype
,但 Chrome 仍然抱怨:
“资源解释为字体,但使用 MIME 类型应用程序/八位字节流传输。”
有人知道吗?
Keith Shaw在 2017 年 6 月 22 日的评论更新:
截至 2017 年 2 月,RFC8081是提议的标准。它定义了字体的顶级媒体类型,因此 WOFF 和 WOFF2 的标准媒体类型如下:
font/woff
font/woff2
2011 年 1 月,Chromium宣布同时承认
application/x-font-woff
作为 WOFF 的 mime 类型。我知道这个变化现在在 Chrome 测试版中,如果还不稳定,它应该不会太远。
对我来说,下一个一直在 .htaccess 文件中工作。
AddType font/ttf .ttf
AddType font/eot .eot
AddType font/otf .otf
AddType font/woff .woff
AddType font/woff2 .woff2
会的application/font-woff
。
参见http://www.w3.org/TR/WOFF/#appendix-b(W3C候选推荐 2011 年 8 月 4 日)
和http://www.w3.org/2002/06/registering-mediatype.html
来自 Mozilla css font-face notes
在 Gecko 中,Web 字体受到相同的域限制(字体文件必须与使用它们的页面位于同一域中),除非使用 HTTP 访问控制来放宽此限制。注意:由于没有为 TrueType、OpenType 和 WOFF 字体定义 MIME 类型,因此不考虑指定文件的 MIME 类型。
将字体 mime 类型添加到 .NET/IIS 的参考
通过 web.config
<system.webServer>
<staticContent>
<!-- remove first in case they are defined in IIS already, which would cause a runtime error -->
<remove fileExtension=".woff" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff" mimeType="font/woff" />
<mimeMap fileExtension=".woff2" mimeType="font/woff2" />
</staticContent>
</system.webServer>
通过 IIS 管理器
NGINX 解决方案
文件
/etc/nginx/mime.types
或者
/usr/local/nginx/conf/mime.types
添加
font/ttf ttf;
font/opentype otf;
font/woff woff;
font/woff2 woff2;
application/vnd.ms-fontobject eot;
消除
application/octet-stream eot;
参考
RFC @02.2017
https://www.rfc-editor.org/rfc/rfc8081#page-15
https://www.iana.org/assignments/media-types/media-types.xhtml
感谢迈克富尔彻
截至 2017 年 2 月,RFC8081是提议的标准。它定义了字体的顶级媒体类型,因此 WOFF 和 WOFF2 的标准媒体类型如下:
font/woff
font/woff2
注意:这个答案在当时是正确的,但在 2017年发布RFC 8081时变得过时了
没有font
MIME 类型!因此,font/xxx
总是错的。
为我做的事情是将其添加到我的 mime_types.rb 初始化程序中:
Rack::Mime::MIME_TYPES['.woff'] = 'font/woff'
并清除缓存
rake tmp:cache:clear
在重新启动服务器之前。
来源:https ://github.com/sstephenson/sprockets/issues/366#issuecomment-9085509
将以下内容添加到您的 .htaccess
AddType font/woff woff
祝你好运
这是一个有用的 mimetypes 列表
我知道这篇文章有点老了,但是在花了很多时间试图让字体在我的 nginx 本地机器上工作并尝试了大量的解决方案之后,我终于找到了一个对我有用的解决方案。
location ~* \.(eot|otf|ttf|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
在括号内,您可以放置字体的扩展名或通常要加载的文件。例如,我也将它用于字体和图像(png、jpg 等),所以不要混淆这个解决方案仅适用于字体。
只需将其放入您的 nginx 配置文件,重新启动,我希望它也适用于您!
也许这会对某人有所帮助。我看到在 IIS 7.ttf
上已经是一种已知的 mime 类型。它配置为:
application/octet-stream
所以我只是为所有 CSS 字体类型(.oet
, .svg
, .ttf
, .woff
)添加了它,并且 IIS 开始为它们提供服务。Chrome 开发工具也不会抱怨重新解释类型。
干杯,迈克尔
对于所有解决方案 index.php 都允许删除表单 url 和 woff 文件。在 .htaccess 文件中写入以下代码并对您的 application/config/config.php 文件进行此更改: $config['index_page'] = '';
仅适用于 Linux 托管服务器。 .htaccess 文件详细信息
AddType font/ttf .ttf
AddType font/eot .eot
AddType font/otf .otf
AddType font/woff .woff
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
关闭:
尝试添加:
AddType application/vnd.ms-fontobject .eot
AddType application/octet-stream .otf .ttf
IIS 自动将 .ttf 定义为 application/octet-stream 似乎工作正常,fontshop 建议将 .woff 定义为 application/octet-stream
Mime 类型可能不是您唯一的问题。如果字体文件托管在 S3 或其他域上,您可能还会遇到 Firefox 不会从不同域加载字体的问题。使用 Apache 可以轻松修复,但在 Nginx 中,我读到您可能需要将字体文件编码为 base-64 并将它们直接嵌入到字体 css 文件中。