2016-04-18 3 views
0

.htaccessに新しいルールを追加するか、YII2のindex.phpにコードを追加する必要がありますか?yii2のgzipを有効にするには?

私のサイトは共有ホスティングになっています。

.cssと.jsファイルのみを圧縮したいです。私はすべての反応を圧縮したくありません。

答えて

1

イベントハンドラを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(); 
0

私は.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> 

設定リスト:https://github.com/h5bp/server-configs

関連する問題