Apache .htaccess:提供预压缩的@ font-face字体

时间:2012-03-06 18:27:03

标签: apache .htaccess fonts compression

我需要Apache来提供预压缩字体(不使用deflate)。

/ path_to / fonts /文件夹中的.htaccess看起来像

RewriteEngine On
RewriteBase /path_to/fonts/
RewriteCond %{HTTP:Accept-Encoding} .*gzip.*


RewriteRule (.*)\.ttf $1.ttf.gz


AddEncoding x-gzip gz

RemoveType application/x-gzip .gz

响应标题:

Accept-Ranges   bytes
Connection  Keep-Alive
Content-Encoding    **gzip**
Content-Length  **31709**
Content-Type    **text/plain**
Date    Tue, 06 Mar 2012 18:14:51 GMT
Etag    "7200000008e241-7bdd-4ba954a7395a8"
Keep-Alive  timeout=5, max=99
Last-Modified   Tue, 06 Mar 2012 16:11:08 GMT
Server  Apache/2.2.11 (Win32) PHP/5.2.9
Vary    Accept-Encoding 

内容长度表示31709,这将是压缩大小,但我无法下载。

你能提一下吗?

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。它主要有点夸张。

除非客户端支持gzip,否则它不会设置类型和编码。同时声明所使用的模块,如果不支持所有模块,则不会发生任何事情。

文件夹结构:

fonts/  
  Shanti-Regular.ttf.gz  
  Federo-Regular.ttf.gz  
  Shanti-Regular.ttf  
  Federo-Regular.ttf  
  .htaccess  

然后.htaccess包含:

# Rewrite URLs to add gzipped version of font when it exits.
<IfModule mod_rewrite.c>
<IfModule mod_mime.c>
  RewriteEngine on

  #Serve gzip compressed TTF files if they exist and the client accepts gzip.
  RewriteCond %{HTTP:Accept-encoding} gzip
  RewriteCond %{REQUEST_FILENAME}\.gz -s
  RewriteRule ^(.*)\.ttf $1\.ttf\.gz [QSA]

  # update the response header of compressed file
  # makes browser think mod_gzip did it.
  <FilesMatch "\.ttf\.gz$">
    AddEncoding gzip .gz
    ForceType "application/x-font-ttf"
  </FilesMatch>

</IfModule>
</IfModule>