简体   繁体   English

如何在Apache中对HTML,CSS和JS文件使用gzip压缩?

[英]How to use gzip compression in apache for html, css and js files?

When an Apache server supports gzip compression, how can from PHP send a CSS file to the client. 当Apache服务器支持gzip压缩时,如何从PHP发送CSS文件到客户端。 Is this using .htaccess or with a library? 这是使用.htaccess还是使用库? If is a simple .htaccess line, do provide it! 如果是简单的.htaccess行,请提供它!

Thanks a lot 非常感谢

You want mod_deflate . 您需要mod_deflate There's no need to introduce PHP to serve compressed content with Apache. 无需引入PHP即可通过Apache提供压缩内容。

Sometimes there IS a need(reason) to introduce it -- if you really want to use it. 有时有必要(理由)对其进行介绍-如果您​​确实想使用它。

Here's the .htaccess addition you might make: 这是您可能会添加的.htaccess文件:

///////compress text, html, css, xml, & javascript://///// ////////压缩文本,html,css,xml和javascript:///////

AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType定义文本/纯文本

AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text / html

AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text / xml

AddOutputFilterByType DEFLATE text/css AddOutputFilterByType定义文字/ css

AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE应用程序/ xml

AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application / xhtml + xml

AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application / rss + xml

AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE应用程序/ javascript

AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE应用程序/ x-javascript

////Or, compress by extension:///// ////或按扩展名压缩/////

<Files *.html> SetOutputFilter DEFLATE </Files> <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. 如果您的主机由于某种原因在Apache上禁用了该选项,则.htaccess方法会将您的网站“压缩”为500条错误消息。 8) 8)

However, if zlib is built into your php, you can use php.ini in your root (or subfolders) to set 但是,如果zlib内置在php中,则可以在根目录(或子文件夹)中使用php.ini进行设置

zlib.output_compression = 1 zlib.output_compression = 1

and that will compress your php scripts, but no others. 并且将压缩您的php脚本,但不会压缩其他脚本。 If you still really want your css, js, xml (etc) files compressed though you can check this out: 如果您仍然确实希望压缩css,js,xml(等)文件,尽管可以执行以下操作:

http://www.thewebdevelopmentblog.com/2008/10/tip-speed-up-your-websites-using-gzip-and-merging-files/ 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 托德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM