如何在apache中使用gzip压缩html、css和js文件?
当 Apache 服务器支持 gzip 压缩时,如何从 PHP 发送 CSS 文件到客户端。这是使用 .htaccess 还是使用库?如果是简单的 .htaccess 行,请提供它!
多谢
When an Apache server supports gzip compression, how can from PHP send a CSS file to the client. Is this using .htaccess or with a library? If is a simple .htaccess line, do provide it!
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
mod_deflate
。无需引入 PHP 来使用 Apache 提供压缩内容。You want
mod_deflate
. There's no need to introduce PHP to serve compressed content with Apache.有时需要(理由)引入它——如果你真的想使用它。
以下是您可能添加的 .htaccess:
///////compress text, html, css, xml, & javascript:///////
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
////或者,按扩展名压缩://///
设置输出过滤器放气
//////////////////
如果您的主机出于某种原因在 Apache 上禁用了该选项,那么 .htaccess 方法会将您的站点“压缩”为 500 错误消息。 8)
但是,如果 zlib 内置于您的 php 中,您可以在根目录(或子文件夹)中使用 php.ini 设置
zlib.output_compression = 1
,这将压缩您的 php 脚本,但不会压缩其他脚本。如果您仍然希望压缩 css、js、xml(等)文件,您可以查看以下内容:
http://www.thewebdevelopmentblog.com/2008/10/tip-speed-up-your-websites-using-gzip-and-merging -files/
它不是一个理想的/站点范围的解决方案,但如果您只有几个大文件困扰您......那么您就可以了!
希望它能帮助别人!
托德
Sometimes there IS a need(reason) to introduce it -- if you really want to use it.
Here's the .htaccess addition you might make:
///////compress text, html, css, xml, & javascript:///////
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
////Or, compress by extension://///
<Files *.html>
SetOutputFilter DEFLATE
</Files>
//////////////////
If your host has the option disabled on Apache for some reason, then the .htaccess approach will "compress" your site into a 500 error message. 8)
However, if zlib is built into your php, you can use php.ini in your root (or subfolders) to set
zlib.output_compression = 1
and that will compress your php scripts, but no others. If you still really want your css, js, xml (etc) files compressed though you can check this out:
http://www.thewebdevelopmentblog.com/2008/10/tip-speed-up-your-websites-using-gzip-and-merging-files/
it's less than an ideal/site wide solution, but if you have only a few large files that are bugging you... then there you go!
Hope it helps someone!
Todd