dzh*_*zhi 22 .htaccess gzip mod-deflate apache-2.2
如何通过 .htaccess 启用 Gzip 或 Deflate 压缩,现在哪个最好?需要的代码示例。
dzh*_*zhi 15
HTML5 Boilerplate ( http://html5boilerplate.com ) 提供了似乎是最好和最有效的解决方案设置以及许多其他解决方案,如缓存、mime 类型等。强烈推荐。
<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/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>
Run Code Online (Sandbox Code Playgroud)
编辑:由于这个问题和答案在几年后不断得到支持,我将 H5BP 服务器配置链接放在更完整的优化上。
编辑:固定链接到https://github.com/h5bp/server-configs-apache
mor*_*ant 12
请参阅 Apache mod_deflate文档,特别是“压缩除图像之外的所有内容”示例。它对我来说效果很好,并将.htaccess按如下方式放入文件中:
<IfModule mod_deflate.c>
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>
Run Code Online (Sandbox Code Playgroud)
而且,当然,请确保您的httpd.conf文件中包含以下内容以启用mod_deflate:
LoadModule deflate_module libexec/apache2/mod_deflate.so
Run Code Online (Sandbox Code Playgroud)
小智 9
我使用以下添加到.htaccess我的public_html目录根目录的文件中的方式对我网站上的静态资产(按 MIME 类型)启用了 deflate :
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
</ifModule>
Run Code Online (Sandbox Code Playgroud)
您也可以通过文件扩展名启用它,尽管我没有方便的语法。
| 归档时间: |
|
| 查看次数: |
56091 次 |
| 最近记录: |