简体   繁体   English

如何启用laravel 4 gzip?

[英]how to enable laravel 4 gzip?

I have two .htaccess file in my laravel 4 project,one in root and another one in public folder.I copied this line in first:我的 Laravel 4 项目中有两个 .htaccess 文件,一个在 root 中,另一个在 public 文件夹中。我首先复制了这一行:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

because II don't wanna type /public after domain name,and It works,but I copy the code for enabling gzip in second .htaccess it causes 500 internal server error.因为我不想在域名后输入 /public 并且它可以工作,但是我复制了在第二个 .htaccess 中启用 gzip 的代码,它会导致 500 内部服务器错误。 I copy below lines from gziptest.com/faq and also I tried many other files but none of them worked for me.我从gziptest.com/faq复制了以下行,我还尝试了许多其他文件,但没有一个对我有用

   AddOutputFilterByType DEFLATE text/html
   AddOutputFilterByType DEFLATE text/plain
   AddOutputFilterByType DEFLATE text/xml
   AddOutputFilterByType DEFLATE application/x-javascript
   AddOutputFilterByType DEFLATE application/javascript
   AddOutputFilterByType DEFLATE text/javascript
   AddOutputFilterByType DEFLATE text/x-js
   AddOutputFilterByType DEFLATE text/css

   # Highest 9 - Lowest 1
   DeflateCompressionLevel 9

   # Skip browsers with known problems
   BrowserMatch ^Mozilla/4 gzip-only-text/html
   BrowserMatch ^Mozilla/4\.0[678] no-gzip
   BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

please help me?请帮我?

I was having the same problem and then I went and tried something from the folks at html5 boilerplate and it worked for me. 我遇到了同样的问题,然后我去尝试了html5样板中的一些方法 ,它对我有用

Here's the snippet: 这是代码段:

<IfModule mod_deflate.c>

    # Force compression for mangled headers.
    # https://developer.yahoo.com/blogs/ydn/pushing-beyond-gzipping-25601.html

    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # Map certain file types to the specified encoding type in order to
    # make Apache serve them with the appropriate `Content-Encoding` HTTP
    # response header (this will NOT make Apache compress them!).

    # If the following file types wouldn't be served without the appropriate
    # `Content-Enable` HTTP response header, client applications (e.g.:
    # browsers) wouldn't know that they first need to uncompress the response,
    # and thus, wouldn't be able to understand the content.

    # http://httpd.apache.org/docs/current/mod/mod_mime.html#addencoding

    <IfModule mod_mime.c>
        AddEncoding gzip              svgz
    </IfModule>

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # Compress all output labeled with one of the following media types.

    # IMPORTANT: For Apache versions below 2.3.7 you don't need to enable
    # `mod_filter` and can remove the `<IfModule mod_filter.c>` & `</IfModule>`
    # lines as `AddOutputFilterByType` is still in the core directives.

    <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-web-app-manifest+json" \
                                      "application/xhtml+xml" \
                                      "application/xml" \
                                      "font/opentype" \
                                      "image/svg+xml" \
                                      "image/x-icon" \
                                      "text/cache-manifest" \
                                      "text/css" \
                                      "text/html" \
                                      "text/javascript" \
                                      "text/plain" \
                                      "text/vtt" \
                                      "text/x-component" \
                                      "text/xml"
    </IfModule>

</IfModule>

This is kind of an old question, so this probably won't be much help to the OP, but I came across this while dealing with the same issue (500 internal server error after modifying the .htaccess) and thought it might be worth mentioning in case anyone else winds up here with the same problem. 这是一个老问题,所以对OP可能没有太大帮助,但是我在处理同一问题(修改.htaccess后出现500个内部服务器错误)时遇到了这个问题,并认为可能值得一提万一其他人也遇到同样的问题。 After checking the error log, I found this error: 检查错误日志后,我发现此错误:

...Invalid command 'AddOutputFilterByType', perhaps misspelled or defined by a module not included in the server configuration ...无效的命令“ AddOutputFilterByType”,可能是拼写错误或由服务器配置中未包含的模块定义的

So after Googling that, I found this in the Apache docs on upgrading to 2.4 from 2.2 , where it says: 因此,在Google搜索之后,我在Apache文档中从2.2升级到2.4的过程中发现了一点:

Invalid command 'AddOutputFilterByType', perhaps misspelled or defined by a module not included in the server configuration - AddOutputFilterByType has moved from the core to mod_filter, which must be loaded. 无效命令'AddOutputFilterByType',可能是服务器配置中未包含的模块拼写错误或定义了-AddOutputFilterByType已从内核移至必须加载的mod_filter。

I am running 2.4, so I checked httpd.conf and uncommented LoadModule filter_module modules/mod_filter.so and that did the trick. 我正在运行2.4,所以我检查了httpd.conf和未注释的LoadModule filter_module modules/mod_filter.so ,并且成功了。

You may try something like this (in your .htaccess file): 您可以尝试这样的操作(在您的.htaccess文件中):

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>

Just put the gzip code after your re-write rules. 只需将gzip代码放在re-write规则之后即可。 You may also check this link and this package . 您也可以检查此链接此软件包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM