【问题标题】:How do I site optimization php files with gzip compression using .htaccess or php.ini?如何使用 .htaccess 或 php.ini 使用 gzip 压缩来优化 php 文件?
【发布时间】:2012-05-13 08:48:16
【问题描述】:

我正在使用http://html5boilerplate.com/ 提供的.htaccess,它可以正确压缩 .html、.css 和 .js 文件,但我无法压缩 .php 文件,除非我添加 <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>到页面顶部。我要做的最后一件事是将该行添加到一百多页。

我一直在使用以下两个站点来验证 gzip 压缩:

以下建议也无效:

我还尝试了以下方法 (http://perishablepress.com/fast-effective-php-compression/):

<?php // placed in a gzip_start.php file
ob_start("ob_gzhandler");
?>

<?php // placed in a gzip_stop.php file
ob_flush();
?>

# Placed in the .htaccess filew
# dual file includes for PHP compression
php_value  auto_prepend_file  /specify/full/path/to/gzip_start.php
php_value  auto_append_file   /specify/full/path/to/gzip_stop.php

这是我的.htaccess 文件的 Gzip 部分:

# ----------------------------------------------------------------------
# Gzip compression
# ----------------------------------------------------------------------

<IfModule mod_deflate.c>

# Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
<IfModule mod_setenvif.c>
  <IfModule mod_headers.c>
    SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s,?\s(gzip|deflate)?|X{4,13}|~{4,13}|-{4,13})$ HAVE_Accept-Encoding
    RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
  </IfModule>
</IfModule>

# HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
<IfModule filter_module>
  FilterDeclare   COMPRESS
  FilterProvider  COMPRESS  DEFLATE resp=Content-Type /text/(html|css|javascript|plain|x(ml|-component))/
  FilterProvider  COMPRESS  DEFLATE resp=Content-Type /application/(javascript|json|xml|x-javascript)/
  FilterChain     COMPRESS
  FilterProtocol  COMPRESS  change=yes;byteranges=no
</IfModule>

<IfModule !mod_filter.c>
  # Legacy versions of Apache
  AddOutputFilterByType DEFLATE text/text text/html text/plain text/css application/json
  AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript 
  AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
</IfModule>

# Webfonts and SVG:
  <FilesMatch "\.(ttf|otf|eot|svg|php)$" >
    SetOutputFilter DEFLATE
  </FilesMatch>
</IfModule>

【问题讨论】:

  • 如果您的项目中有hundred plus pages,那么您可能需要重新考虑您的设计模式
  • 访问php文件时服务器发送的Content-Type是什么?
  • @Gerben 很好的问题。 Content-Type:text/html; charset=UTF-8
  • 您解决了吗?你检查过zlib.output_compression 的值吗?

标签: php apache .htaccess gzip


【解决方案1】:

像这样编辑您的 .htaccess 文件

# 压缩文本、html、javascript、css、xml: AddOutputFilterByType DEFLATE 文本/纯文本 AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE 文本/xml AddOutputFilterByType DEFLATE 文本/css AddOutputFilterByType DEFLATE 应用程序/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE 应用程序/javascript AddOutputFilterByType DEFLATE 应用程序/x-javascript # 或者,按扩展名压缩某些文件类型: &lt文件 *.html &gt SetOutputFilter DEFLATE &lt/文件&gt &lt文件 *.css&gt SetOutputFilter DEFLATE &lt/文件&gt &lt文件 *.js &gt SetOutputFilter DEFLATE &lt/文件&gt &lt文件 *.img &gt SetOutputFilter DEFLATE &lt/文件&gt

【讨论】:

  • 仅仅发布没有任何上下文或解释的代码是不够的。我建议您阅读 how to answer 并展开它。
  • 图像文件不应该被压缩,因为它们已经被压缩了。这样做只会导致 CPU 性能下降,有时还会导致图像损坏。
【解决方案2】:

如果您有带有 .php 扩展名的视图。您必须将其添加到 .htaccess 文件中

<files *.php >
SetOutputFilter DEFLATE
 </files>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2012-09-15
    相关资源
    最近更新 更多