php 使用压缩css文件,PHP-使用GZIP压缩静态CSS文件

所以我有一个CSS文件style.css.在同一目录中,我有images /文件夹.

如何制作一个从另一个文件夹压缩style.css的脚本?

现在我有这个:

if(isset($_GET['css'])) $file = array('url' => htmlspecialchars($_GET['css']), 'type' => 'text/css');

if(isset($_GET['js'])) $file = array('url' => htmlspecialchars($_GET['js']), 'type' => 'application/javascript');

if(!isset($file)) exit();

header('Content-type: '.$file['type']);

header('Cache-Control: max-age='.(60*60*6).', must-revalidate');

// whitespace+comment removal, in case gzip is off

function compress($buffer){

global $file;

$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), ' ', $buffer);

return $buffer;

}

if(extension_loaded('zlib'))

if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler");

else ob_start("compress");

include($file['url']);

ob_end_flush();

?>

并使用像这样的东西:

@import "http://mysite.com/lib/c.php?css=../style.css";

一切正常,只是css文件中图像的路径不再起作用.

我认为这是因为(images / bg.jpg)成为(../images/bg.jpg)

我可以解决此问题而不必将c.php脚本移动到与样式表相同的目录中吗?

解决方法:

如果您的Web服务器具有此类功能,则可以使用URL重写使内容保持整洁.

使用mod_rewrite的示例:

RewriteRule ^images/style\.css$lib/c.php?css=../style.css

然后,您可以简单地:

@import "http://mysite.com/images/style.css";

我还建议您对脚本进行第二次查看.如果加载此URL,会发生什么?

http://mysite.com/lib/c.php?css=.htpasswd

标签:compression,gzip,css,html,php

来源: https://codeday.me/bug/20191209/2096512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值