【问题标题】:GZIP compression not working- htaccess file might be wrong?GZIP 压缩不起作用 - htaccess 文件可能是错误的?
【发布时间】:2024-01-08 01:13:01
【问题描述】:

让我的服务器压缩我的文件时,我遇到了最糟糕的情况。我使用 ipage 作为我的主机,它正在运行 apache II。当我查看谷歌开发人员工具中的“标题”区域时,它说文件“接受编码”:gzip、deflate、sdch,但 pagespeed 洞察力仍然告诉我我的文件没有压缩。这是我的 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>

【问题讨论】:

    标签: php html apache .htaccess gzip


    【解决方案1】:

    创建一个名为 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。 :) 我不确定将黑斜线用作分隔符/分隔符。

    【讨论】:

    【解决方案2】:

    检查您没有通过在下载过程中解压缩内容的代理吗?

    我刚刚检查了您使用 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.jshttp://www.uniconutrition.com/css/bootstrap.css

    我怀疑您的网络服务器使用的是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.uniconutrition.com,但是这并没有发生,所以它的其余部分也被忽略是理所当然的。请与您的房东核实。

    【讨论】:

    • 我在 htaccess 中有这个用于重定向 - 它是否会导致问题?选项 +FollowSymlinks RewriteEngine on rewritecond %{http_host} ^uniconutrition.com [nc] rewriterule ^(.*)$ uniconutrition.com/$1 [r=301,nc]
    • 不太可能,因为这是重定向规则。
    • 这是一个菜鸟问题-但我似乎无法在浏览器中使用它之前的“http://”打开我的任何文件......所以我认为这意味着你是对的?我该如何解决这个问题?
    • http:// 刚刚被删除-我认为这是您提到的代理的结果?
    • 重定向规则的目的是什么?乍一看似乎没什么用(将主机名以 uniconutrition.com 开头的所有内容重定向到 uniconutrition.com),这意味着您的主机根本不尊重 htaccess 文件
    最近更新 更多