简体   繁体   English

PHP - 发送gzip压缩的JS / CSS

[英]PHP - Sending gzip compressed JS/CSS

I created a style.css.php file with this code: 我用这段代码创建了一个style.css.php文件:

<?php

  $gzip = (ob_get_length() === false && !ini_get("zlib.output_compression") && ini_get("output_handler") != "ob_gzhandler" && extension_loaded("zlib") && substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && !headers_sent());

  if(!$gzip) header('Location: style.css');

  header('Content-type: text/css');
  header('Cache-Control: no-cache');
  header('Expires: Mon, 1 Jan 1901 04:20:00 GMT');

  ob_start('ob_gzhandler');

  include "style.css";
?>

What do you think? 你怎么看? Is this a good way to compress js/css files? 这是压缩js / css文件的好方法吗? Is there a better way to do this? 有一个更好的方法吗? I'm doing this for a public app. 我正在为一个公共应用程序这样做。 that can be downloaded by anyone. 任何人都可以下载。 So there will be people on shared hosts with gzip disabled 因此,在禁用gzip的共享主机上会有​​人

No, not OK. 不,不行。 There's a lot of things wrong there. 那里有很多错误。 The include, no dying after redirecting, not considering the deflate method, ... 包括,重定向后没有死亡,没有考虑放气方法,......

This is very simple to do with PHP, as the zlib output handler automatically detects the appropriate compression to send to the client (if any); 这与PHP非常简单,因为zlib输出处理程序会自动检测发送给客户端的适当压缩(如果有的话); all you have to do is enable it: 你所要做的就是启用它:

<?php
if (extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler")) {
    ini_set("zlib.output_compression", 1);
}

readfile('style.css');

If you're serving your site using Apache, you can use either mod_gzip or mod_deflate. 如果您使用Apache为您的站点提供服务,则可以使用mod_gzip或mod_deflate。 They are usually available on shared hosts and can be configured in .htaccess files. 它们通常在共享主机上可用,并且可以在.htaccess文件中配置。

Add the following lines to your .htaccess file: 将以下行添加到.htaccess文件中:

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

(ie one per mime-type) (即每个哑剧型一个)

如果配置正确,服务器应该自动执行此操作。

Adam is on the right track but it does not need to be one MIME type per line. Adam处于正确的轨道上,但每行不需要一个MIME类型。 See the Apache2 manual for more info on the AddOutputFilterByType directive . 有关AddOutputFilterByType指令的更多信息,请参阅Apache2手册。

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript text/javascript-x application/javascript

First SET in '.htaccess' '.htaccess'中的第一个SET

RewriteEngine on
RewriteRule style.css style.css.php
<IfModule mod_deflate.c>
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
SetOutputFilter DEFLATE

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch MSI[E] !no-gzip !gzip-only-text/html

SetEnvIfNoCase Request_URI 
.(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>

Recomended create css folder and put files there. 推荐创建css文件夹并将文件放在那里。

With RewriteRule you don't need set header('Content-type: text/css'); 使用RewriteRule,您不需要设置header('Content-type: text/css'); and other functions set gzip on server before php process. 和其他函数在php进程之前在服务器上设置gzip。 the code running more fast now! 代码运行速度更快了!

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

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