【发布时间】:2016-04-18 21:59:15
【问题描述】:
我需要在.htaccess 中添加新规则还是在YII2 的index.php 中添加代码?
我的网站在共享主机上。
我只想压缩 .css 和 .js 文件。我不想压缩所有响应。
【问题讨论】:
我需要在.htaccess 中添加新规则还是在YII2 的index.php 中添加代码?
我的网站在共享主机上。
我只想压缩 .css 和 .js 文件。我不想压缩所有响应。
【问题讨论】:
您可以通过在 index.php 中的 yii\web\Response 上附加事件处理程序来使其工作。
$application = new yii\web\Application($config);
$application->on(yii\web\Application::EVENT_BEFORE_REQUEST, function(yii\base\Event $event){
$event->sender->response->on(yii\web\Response::EVENT_BEFORE_SEND, function($e){
ob_start("ob_gzhandler");
});
$event->sender->response->on(yii\web\Response::EVENT_AFTER_SEND, function($e){
ob_end_flush();
});
});
$application->run();
【讨论】:
我在 .htaccess 中添加了以下规则:
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE "application/atom+xml" \
"application/javascript" \
"application/json" \
"application/ld+json" \
"application/manifest+json" \
"application/rdf+xml" \
"application/rss+xml" \
"application/schema+json" \
"application/vnd.geo+json" \
"application/vnd.ms-fontobject" \
"application/x-font-ttf" \
"application/x-javascript" \
"application/x-web-app-manifest+json" \
"application/xhtml+xml" \
"application/xml" \
"font/eot" \
"font/opentype" \
"image/bmp" \
"image/svg+xml" \
"image/vnd.microsoft.icon" \
"image/x-icon" \
"text/cache-manifest" \
"text/css" \
"text/html" \
"text/javascript" \
"text/plain" \
"text/vcard" \
"text/vnd.rim.location.xloc" \
"text/vtt" \
"text/x-component" \
"text/x-cross-domain-policy" \
"text/xml"
</IfModule>
【讨论】: