有人可以给我一个 htaccess 的示例 gzip 代码吗?
我很想开始尝试 gzip,但是就像我第一次开始学习 php 时经常找到 php.net 一样,apache 文档让我有点困惑。 http://httpd.apache.org/docs/2.0/mod/mod_deflate。 html
我真的很感谢一个示例 htaccess 文件来看看是否有人有一个?
如果我在服务器范围内启用 gzipping,是否还应该在 httpd.conf 文件而不是 htaccess 中更改一些内容?
抱歉,我是 apache 的新手!
编辑:
所以我在 httpd.conf 中启用 mod_deflate ,然后将以下内容放入 ht 访问文件中(作为示例)?
<Location />
# 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
</Location>
I'm keen to get started experimenting with gzip, but like i used to find php.net when i first started learning php, the apache documentation confuses me a bit. http://httpd.apache.org/docs/2.0/mod/mod_deflate.html
I'd really appreciate a sample htaccess file to have a look at if anyone's got one?
Also are there things which should be changed in the httpd.conffile instead of htaccess if I'm enabling gzipping server-wide?
Sorry i'm a newb with apache!
edit:
so i enable mod_deflate in the httpd.conf and then putting the following into an ht access file will work (as an example)?
<Location />
# 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
</Location>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 Apache 中使用 mod_deflate 或 PHP 启用 gzip。该模块本身必须在 Apache 的主配置文件中激活,尽管您可以使用 .htaccess 文件决定按目录压缩哪些文件(除非主配置禁止使用
AllowOverride None
或类似内容)。您包含的链接提供了示例。您还可以通过将
zlib.output_compression = On
添加到 php.ini 文件来仅在 PHP 中启用它。You can enable gzip either in Apache, with mod_deflate or with PHP. The module itself must be activated in Apache's main config file, though you can decide which files are compressed on a per-directory basis using .htaccess files (unless the main configuration forbids with
AllowOverride None
or similar). The link you included provides examples.You can also enable it only in PHP by adding
zlib.output_compression = On
to your php.ini file.