Php 通过.htaccess进行的Gzip压缩不工作

Php 通过.htaccess进行的Gzip压缩不工作,php,html,.htaccess,web,gzip,Php,Html,.htaccess,Web,Gzip,我一直在创建一个网站,并通过gtmetrix.com检查它的速度。我使用了下面的.htaccess文件来压缩.js、.css和更多文件 <ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip

我一直在创建一个网站,并通过gtmetrix.com检查它的速度。我使用了下面的
.htaccess
文件来压缩
.js、.css
和更多文件

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include handler ^cgi-script$
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

Options Indexes
IndexOptions FancyIndexing

ExpiresActive On
ExpiresDefault "access plus 7 days"
ExpiresByType application/javascript A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/javascript A2592000
ExpiresByType text/html A2592000
ExpiresByType text/xml A2592000
ExpiresByType text/css A2592000
ExpiresByType text/plain A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/bmp A2592000
ExpiresByType application/x-shockwave-flash A2592000

<FilesMatch "\.(html?|txt)$">
  ForceType 'text/html; charset=UTF-8'
</FilesMatch>

<FilesMatch "\.(css)$">
  ForceType 'text/css; charset=UTF-8'
</FilesMatch>

<FilesMatch "\.(js)$">
  ForceType 'text/javascript; charset=UTF-8'
</FilesMatch>

<FilesMatch "\.(css|js)$">
  Header append Vary Accept-Encoding
</FilesMatch>

但是我已经对我的
.htaccess
文件进行了
gzip
压缩。我不知道这里出了什么问题,顺便说一句,我对这种编码风格非常陌生。因此,请告诉我是否有任何错误。

我没有使用mod_gzip,但这是我通过mod_deflate访问gzip.js、.css和其他文件的.htaccess:

#Gzip
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x- javascript application/javascript
</ifmodule>
#End Gzip
#Gzip
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
#结束Gzip
如果您还没有意识到,这对于检查下载的内容是否是gzip非常有用。单击Firebug中的“Net”选项卡,然后执行shift-f5以使Firefox重新加载所有的.js和.css文件,使它们显示在Net面板中。单击.js或.css文件旁边的“+”并单击“标题”选项卡。如果响应是gzip,您将在响应头部分看到“Content Encoding gzip”。我想象IE、Safari和Chrome的等价物也提供了同样的功能


有很多值得注意的地方。我认为它只适用于IE6 SP1用户。

这里有一个用于文件扩展名的语法

<IFModule mod_deflate.c>
    <filesmatch "\.(js|css|html|jpg|png|php)$">
        SetOutputFilter DEFLATE
    </filesmatch>
</IFModule>

SetOutputFilter放气

在我的例子中,mod_gzip或mod_deflate即使在启用并将其写入htaccess文件后也无法工作

然后,我在php文件中使用了'ob_gzhandler',这帮助我压缩了php和html文件。这比不上mod_gzip或mod_deflate,但可以临时为您服务,以防您在其他方面运气不佳

在加载任何内容之前,我在顶部的index.php文件中使用的代码是

<? ob_start("ob_gzhandler"); ?>


BTW,确保安装了mod_gzip和/或mod_gzip Apache模块。此外,您可能希望检查Apache错误日志中是否存在任何适用的错误。我在.htaccess文件中使用了您的脚本,但出现了内部服务器错误。您知道是否有办法在.js文件中实现这一点吗?
<? ob_start("ob_gzhandler"); ?>