在HTML文档中写什么来启用GZip?

时间:2014-08-26 06:33:30

标签: php html .htaccess gzip

我在我的网站中添加了以下代码以启用GZIP压缩,但是当我正在测试我的webpage时,它会在在线GTCompression测试网站上给出以下错误消息。请告诉我,我需要在HTML文件中添加任何内容吗?

Web Page        The http server hasn't sent out a "Content-Encoding" header.
Your Browser    Your browser is accepting compressed content.

.htaccess FIle

    <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>

2 个答案:

答案 0 :(得分:1)

尝试以下代码

<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*)+|[X~-]{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
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/css
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/plain
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/xml
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/x-component
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/javascript
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/json
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xml
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xhtml+xml
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/rss+xml
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/atom+xml
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/vnd.ms-fontobject
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/svg+xml
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/x-font-ttf
            FilterProvider  COMPRESS  DEFLATE resp=Content-Type $font/opentype
            FilterChain     COMPRESS
            FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no
          </IfModule>

          <IfModule !mod_filter.c>
            # Legacy versions of Apache
            AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
            AddOutputFilterByType DEFLATE application/javascript
            AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
            AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml 
            AddOutputFilterByType DEFLATE application/atom+xml
            AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject 
            AddOutputFilterByType DEFLATE application/x-font-ttf font/opentype
          </IfModule>
        </IfModule>

答案 1 :(得分:1)

您需要将以下行添加到.htaccess文件中:

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>