【问题标题】:HTML template gzip doesn't work (PageSpeed Insights)HTML 模板 gzip 不起作用 (PageSpeed Insights)
【发布时间】:2014-03-09 12:12:04
【问题描述】:

我想问一下如何在 000webhost.com 上的 HTML 模板上进行压缩我创建了 .htaccess 文件并添加了这个:

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

     <IfModule mod_expires.c>
      ExpiresActive On
      ExpiresByType image/jpg "access 1 year"
      ExpiresByType image/jpeg "access 1 year"
      ExpiresByType image/gif "access 1 year"
      ExpiresByType image/png "access 1 year"
      ExpiresByType text/css "access 1 month"
      ExpiresByType text/html "access 1 month"
      ExpiresByType application/pdf "access 1 month"
      ExpiresByType text/x-javascript "access 1 month"
      ExpiresByType application/x-shockwave-flash "access 1 month"
      ExpiresByType image/x-icon "access 1 year"
      ExpiresDefault "access 1 month"
    </IfModule>

Google 的 PageSpeed Insights 告诉我我没有启用 gzip 压缩,所以我不知道如何正确启用它。

这是我的website

一般来说,我想知道如何提高我网站的性能?嗯,根据 PageSpeed Insights,目前我的网站有 56/100 移动设备和 73/100

【问题讨论】:

    标签: html .htaccess gzip


    【解决方案1】:

    我使用了 defalte mod 而不是 gzip:

    代码如下:

    # BEGIN DEFLATE COMPRESSION
    <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
    </IfModule>
    # END DEFLATE COMPRESSION
    

    对于过期标头:

    # Add Expire headers
    <IfModule mod_expires.c>
    <Filesmatch "\.(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$">
      ExpiresActive on
      ExpiresDefault "access plus 30 days"
    </Filesmatch>
    </IfModule>
    

    参考文献:

    1. https://dzone.com/articles/steps-improve-your-pagespeed-insight-score
    2. http://www.quickregisterseo.com/improve-google-page-speed-score-wordpress-without-plugins/

    我网站的当前分数: 台式机:91 手机:83

    【讨论】:

      【解决方案2】:

      来自https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess 将是一种更正常的方式。

      <IfModule mod_deflate.c>
      
          # Force compression for mangled headers.
          # http://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>
      
          # Compress all output labeled with one of the following MIME-types
          # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
          #  and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
          #  as `AddOutputFilterByType` is still in the core directives).
          <IfModule mod_filter.c>
              AddOutputFilterByType DEFLATE application/atom+xml \
                                            application/javascript \
                                            application/json \
                                            application/ld+json \
                                            application/rss+xml \
                                            application/vnd.ms-fontobject \
                                            application/x-font-ttf \
                                            application/x-web-app-manifest+json \
                                            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>
      

      整个文件都值得一读,里面有很多好东西。

      【讨论】:

      • 我不确定这是否有效,或者我做错了什么,但它不会改变任何东西。我仍然收到有关压缩的消息,并且在移动设备和台式机上的评级相同。也许它不起作用,因为我使用免费托管或我的 HTML 文档被窃听?
      • ifmodule 位确保代码仅在安装并激活正确的 Apache 模块时运行。您可能需要使用 a2enmod 打开它们。如果它的免费托管,你可能是我们的运气。
      猜你喜欢
      • 2012-11-24
      • 2015-03-20
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多