【问题标题】:Can't make my Gzip to work无法让我的 Gzip 工作
【发布时间】:2013-05-26 09:41:02
【问题描述】:

我尝试了各种变体,但我的 gzip 无法正常工作。 我的网站有简单的 html 文件(不是 WP)。我承认我真的对我看到的所有不同的代码建议感到困惑。我需要知道什么是正确的代码,以及它没有被压缩的原因是代码还是我需要要求主机更改的东西。这是我的 .htaccess 中的代码:

<filesMatch ".(ico|html|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
SetOutputFilter DEFLATE
Header set Cache-Control "public"
Header set Expires "Thu, 16 Apr 2015 20:00:00 GMT"
Header unset ETag
FileETag None
</filesMatch>

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

#Gzip
    <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
    </ifmodule>
#End Gzip

感谢您的帮助

【问题讨论】:

  • 乍一看我没有发现任何问题。您需要确保的是您的主机已为 httpd 启用了 mod_gzip 和/或 mod_deflate。你正确地使用了 ifmodule 但这当然意味着这些模块的指令只有在它们被加载后才会生效。

标签: html .htaccess gzip


【解决方案1】:

我在我的一些网站中使用此代码,与 deflate 配合使用效果很好。试试看:

# BEGIN GZIP
# mod_gzip compression (legacy, Apache 1.3)
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|xml|txt|css|js)$
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>
# END GZIP

# DEFLATE compression
<IfModule mod_deflate.c>
# Set compression for: html,txt,xml,js,css
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
# Deactivate compression for buggy browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Set header information for proxies
Header append Vary User-Agent
</IfModule>
# END DEFLATE

【讨论】:

  • 1. (旧版,Apache 1.3)- 1.3 是服务器版本吗?如果我的版本不同怎么办? 2. Mozilla 版本也一样——以后的版本呢?
  • 使用 this guide 确保您的 gzip 压缩已启用。
猜你喜欢
  • 2017-12-09
  • 2016-09-02
  • 2015-05-20
  • 2015-10-22
  • 2023-03-24
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多