【问题标题】:How to enable gzip for yii2?如何为 yii2 启用 gzip?
【发布时间】:2016-04-18 21:59:15
【问题描述】:

我需要在.htaccess 中添加新规则还是在YII2 的index.php 中添加代码?

我的网站在共享主机上。

我只想压缩 .css 和 .js 文件。我不想压缩所有响应。

【问题讨论】:

    标签: .htaccess yii yii2


    【解决方案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();
    

    【讨论】:

      【解决方案2】:

      我在 .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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-18
        • 2011-06-10
        • 2015-04-17
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多