如何使用.htaccess或php.ini使用gzip压缩网站优化php文件?

时间:2012-05-03 19:32:25

标签: php apache .htaccess gzip

我正在使用http://html5boilerplate.com/提供的.htaccess正确压缩.html,.css和&amp; .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>

2 个答案:

答案 0 :(得分:0)

编辑您的.htaccess文件,如

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:

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

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

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

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

答案 1 :(得分:0)

如果您有扩展名为.php的观看次数。您必须将此添加到.htaccess文件

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