GZIP压缩不起作用-htaccess文件可能是错误的


GZIP compression not working- htaccess file might be wrong?

让服务器压缩文件的时间最长。我使用ipage作为我的主机,它正在运行apacheII。当我查看谷歌开发工具中的"headers"区域时,它说文件"接受编码":gzip、deflate、sdch,但页面速度洞察仍然告诉我,我的文件没有经过gzip。这是我的htaccess文件代码:

这可能也会有所帮助:http://www.uniconutrition.com/info.php

请帮忙!

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^uniconutrition.com [nc]
rewriterule ^(.*)$ http://www.uniconutrition.com/$1 [r=301,nc]
<IfModule mod_deflate.c>
  <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
      SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)'s*,?'s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
      RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
  </IfModule>
  # Compress all output labeled with one of the following MIME-types
  <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE application/atom+xml '
                                  application/javascript '
                                  application/json '
                                  application/rss+xml '
                                  application/vnd.ms-fontobject '
                                  application/x-font-ttf '
                                  application/xhtml+xml '
                                  application/xml '
                                  font/opentype '
                                  image/svg+xml '
                                  image/x-icon '
                                  text/css '
                                  text/html '
                                  text/plain '
                                  text/x-component '
                                  text/xml
  </IfModule>
</IfModule>

创建一个名为info.php的小文件,输入"phpinfo()"并检查是否包含ZLib。

这一点通常对我有效:

<IfModule mod_deflate.c>
    <IfModule mod_php5.c>
        php_flag zlib.output_compression 1
        php_value zlib.output_compression_level 7
    </IfModule>
    AddOutputFilterByType DEFLATE 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
    <FilesMatch "'.(ttf|otf|eot|svg)$" >
        SetOutputFilter DEFLATE
    </FilesMatch>
</IfModule>

顺便,请注意,我没有包含您所有的mimetype。:)我不确定是否使用黑斜线作为分隔符/间隔符。

检查您是否正在通过一个代理程序,该代理程序正在向下分解内容?

我刚刚检查了你提供的带有cURL的URL(没有代理),当请求HTML时,它显示为gzip’ed,至少:

$ curl -I -H"Accept-Encoding: gzip" http://www.uniconutrition.com/info.php
HTTP/1.1 200 OK
Date: Fri, 19 Jul 2013 14:54:15 GMT
Server: Apache/2
X-Powered-By: PHP/5.2.17
Content-Encoding: gzip
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

然而,CSS和JS不是,例如。http://www.uniconutrition.com/java/main.js和http://www.uniconutrition.com/css/bootstrap.css

我怀疑您的Web服务器使用的是mod_deflate,而不是mod_filter。同样值得注意的是,在您的示例中,您对JS使用了错误的mimetype,应该是application/x-javascript,而不是application/javascript

因此,您可能应该添加一个(更简单的)指令,如下所示:

<IfModule mod_deflate.c>
   AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript
</IfModule>

编辑:

仔细一看,您的重写规则也不起作用;它指定所有流量都应该重定向到www.uniconutration.com,但这并没有发生,因此htaccess文件的其余部分也被忽略是理所当然的。请与您的主人核实。