【问题标题】:Google PageSpeed compressing and .htaccessGoogle PageSpeed 压缩和 .htaccess
【发布时间】:2013-02-08 10:07:30
【问题描述】:

我正在尽我所能加快我的网站速度。我运行了 Google 的 PageSpeed,得到了 83/100。

页面说要压缩资源,比如.js和.css文件。

“使用 gzip 或 deflate 压缩资源可以减少通过网络发送的字节数。”

好吧,然后我用谷歌搜索并编辑了我的 .htaccess:

<IfModule mod_deflate.c>
  <FilesMatch "\.(css|js|xml)$">
    SetOutputFilter DEFLATE
  </FilesMatch>
</IfModule>
<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/png A604800
  ExpiresByType image/gif A604800
  ExpiresByType image/jpg A604800
  ExpiresByType image/jpeg A604800
  ExpiresByType text/javascript A604800
  ExpiresByType application/x-javascript A604800
  ExpiresByType text/css A604800
</IfModule>

当我检查我的网站时:

标题:

Accept-Encoding gzip, deflate

缓存:

Data Size   332
Device  disk
Expires Thu Jan 01 1970 02:00:00

还有我网站中的 PHP:

if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
    ob_start("ob_gzhandler"); 
else 
    ob_start();
header('Vary: Accept');
header('Cache-Control: max-age=28800');

有什么想法吗?

【问题讨论】:

  • 不要在 PHP 中压缩输出,让 Apache 处理所有内容。 mod_deflate 是否已安装并启用?
  • 删除了 PHP 输出。 mod_deflate 已启用。这个网站说我的网站是 gzip 压缩的:whatsmyip.org/http-compression-test 但是,PageSpeed 仍然说“使用 gzip 或 deflate 压缩资源可以减少通过网络发送的字节数。”

标签: .htaccess caching optimization


【解决方案1】:

在您上面的示例代码中,没有为 html 内容指定扩展名,例如.php

我通常通过 mimetype 而非文件扩展名来管理 gzip,例如

  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml \ 
    etc

查看https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess查看完整列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多