php 通过 .htaccess 进行 Gzip 压缩不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5846376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gzip compression through .htaccess not working
提问by Hyman Billy
I have been creating a site and checking it's speed through gtmetrix.com. I used the following .htaccess
file to compress the .js, .css
and many more files.
我一直在创建一个站点并通过gtmetrix.com检查它的速度。我使用以下.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. 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.
但是我已经gzip
对我的.htaccess
文件进行了压缩。我不知道这里出了什么问题,顺便说一下,我对这种编码风格很陌生。所以请让我知道我是否在做任何错误。
回答by Mark Foster
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,但这是我用来通过 mod_deflate gzip .js、.css 和其他文件的 .htaccess:
#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, Firebugis very useful for checking if downloaded content is gzipped. 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. Click the "+" next to the .js or .css file and click the "Headers" tab. If the response is gzipped you will see "Content-Encoding gzip" in the Response Headers section. I image the IE, Safari, and Chrome equivalents offer the same ability.
如果您还没有意识到,Firebug对于检查下载的内容是否被 gzip 非常有用。单击 Firebug 中的“Net”选项卡,然后执行 shift-f5 以让 Firefox 重新加载所有 .js 和 .css 文件,以便它们显示在 Net 面板中。单击 .js 或 .css 文件旁边的“+”,然后单击“标题”选项卡。如果响应是 gzip 压缩的,您将在响应标头部分看到“Content-Encoding gzip”。我想象 IE、Safari 和 Chrome 等价物提供相同的功能。
There is one gotcha regarding gzipping .js and .css files on IE6to watch out for. I think it is only applicable to IE6 SP1 users.
关于在 IE6 上压缩 .js 和 .css 文件有一个问题需要注意。我认为它只适用于 IE6 SP1 用户。
回答by Hamed Momeni
Here is a syntax to use with file extensions.
这是用于文件扩展名的语法。
<IFModule mod_deflate.c>
<filesmatch "\.(js|css|html|jpg|png|php)$">
SetOutputFilter DEFLATE
</filesmatch>
</IFModule>
回答by Nijesh Hirpara
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. 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.
然后,我最终在我的 php 文件中使用了 'ob_gzhandler',它帮助我压缩了我的 php 和 html 文件。这不如 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"); ?>