2016-04-18 15 views
0

Ich muss neue Regeln zu .htaccess hinzufügen oder den Code zu index.php von YII2 hinzufügen?Wie gzip für yii2 aktivieren?

Meine Website ist auf Shared Hosting.

Ich möchte nur .css und .js Dateien komprimieren. Ich möchte nicht alle Antworten komprimieren.

Antwort

1

Sie können es durch Anhängen des Ereignishandlers auf yii \ web \ Response in index.php einrichten.

$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

Ich habe die folgenden Regeln in .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> 

Liste Konfiguration: https://github.com/h5bp/server-configs

Verwandte Themen