简体   繁体   English

通过 .htaccess 的 Gzip 压缩不起作用

[英]Gzip compression through .htaccess not working

I have been creating a site and checking it's speed through gtmetrix.com .我一直在创建一个站点并通过gtmetrix.com检查它的速度。 I used the following .htaccess file to compress the .js, .css and many more files.我使用以下.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>

But when I run the page test it shows the following -----但是当我运行页面测试时,它显示以下内容 -----

Compressing the following resources with gzip could reduce their transfer size by 882B (48% reduction).

Compressing http://wooflux.co.cc/ could save 645B (48% reduction).
Compressing http://wooflux.co.cc/style.css could save 237B (51% reduction).

But I have done gzip compression on my .htaccess file.但是我已经对我的.htaccess文件进行了gzip压缩。 I don't what is getting wrong here, and by the way I'm very new to this coding style.我不知道这里出了什么问题,顺便说一句,我对这种编码风格很陌生。 So please let me know if I'm doing any error or not.所以请让我知道我是否做任何错误。

I have not used mod_gzip but here is my .htaccess I use to gzip.js, .css and other files via mod_deflate:我没有使用 mod_gzip,但这是我的 .htaccess,我通过 mod_deflate 用于 gzip.js、.css 和其他文件:

#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

In case you are not already aware of it, Firebug is very useful for checking if downloaded content is gzipped.如果您还没有意识到这一点, Firebug对于检查下载的内容是否经过 gzip 压缩非常有用。 Click on the "Net" tab in Firebug and then do shift-f5 to get Firefox to reload all the.js and.css files so they show in the Net panel.单击 Firebug 中的“Net”选项卡,然后执行 shift-f5 以使 Firefox 重新加载所有 .js 和 .css 文件,以便它们显示在 Net 面板中。 Click the "+" next to the.js or.css file and click the "Headers" tab.单击 .js 或 .css 文件旁边的“+”,然后单击“Headers”选项卡。 If the response is gzipped you will see "Content-Encoding gzip" in the Response Headers section.如果响应是 gzip 压缩的,您将在 Response Headers 部分看到“Content-Encoding gzip”。 I image the IE, Safari, and Chrome equivalents offer the same ability.我认为 IE、Safari 和 Chrome 等效项提供了相同的功能。

There is one gotcha regarding gzipping.js and.css files on IE6 to watch out for.关于 IE6 上的 gzipping.js 和 .css 文件有一个注意事项。 I think it is only applicable to IE6 SP1 users.我认为它只适用于 IE6 SP1 用户。

Here is a syntax to use with file extensions.这是用于文件扩展名的语法。

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

In my case, mod_gzip or mod_deflate was not working even after making it enabled and writing it in the htaccess file.在我的情况下,mod_gzip 或 mod_deflate 即使在启用它并将其写入 htaccess 文件后也无法正常工作。

Then, I was end up using 'ob_gzhandler' in my php file, which helped me to compress my php and html files.然后,我最终在 php 文件中使用了“ob_gzhandler”,这帮助我压缩了 php 和 html 文件。 This is not as good as mod_gzip or mod_deflate, but serve you temporarily in case if you can't get a luck on other.这不如 mod_gzip 或 mod_deflate 好,但如果您无法在其他方面获得好运,则暂时为您服务。

The code which I used on my index.php file at top, before loading anything is,在加载任何内容之前,我在 index.php 文件上使用的代码是,

<? ob_start("ob_gzhandler"); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM