【问题标题】:How to make gzip work on 1and1 shared host WordPress site如何使 gzip 在 1and1 共​​享主机 WordPress 网站上工作
【发布时间】:2024-01-04 19:04:01
【问题描述】:

YSlow 告诉我应该压缩我的 css,但经过几个小时的修补,我终生无法让 gzip 为我的网站工作。在这一点上,我什至不确定性能提升(会有吗?)是否值得付出努力。

我在 1&1 共享主机帐户上运行 WordPress 网站。

老实说,我真的不知道我在用这些东西做什么,而且似乎无法获得合适的设置。我在几个地方读到 1&1,“未安装模块 Apache mod_deflate 和 mod_gzip。”,所以我认为这是问题的一部分。

我试过以下代码:

这个好像什么都没做:

<IfModule mod_gzip.c>  
    mod_gzip_on         Yes  
    mod_gzip_dechunk    Yes  
    mod_gzip_item_include file          \.(html?|txt|css|js|php|pl)$  
    mod_gzip_item_include handler       ^cgi-script$  
    mod_gzip_item_include mime      ^text\.*  
    mod_gzip_item_include mime      ^application/x-javascript.*  
    mod_gzip_item_exclude mime      ^image\.*  
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*  
</IfModule>  

这会导致 500 错误

<Location />
# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</Location>

这(来自 html5 样板)似乎也没有做任何事情:

<IfModule mod_deflate.c>

  # Force deflate for mangled headers developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
  <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>

  # HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
  <IfModule filter_module>
    FilterDeclare   COMPRESS
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/html
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/css
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/plain
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/x-component
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/javascript
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/json
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xhtml+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/rss+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/atom+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/vnd.ms-fontobject
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/svg+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/x-icon
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/x-font-ttf
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $font/opentype
    FilterChain     COMPRESS
    FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no
  </IfModule>


  <IfModule !mod_filter.c>
    # Legacy versions of Apache
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
  </IfModule>

</IfModule>

这个好像什么都没做……

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

我按照这里找到的教程进行操作

(http://mrrena.blogspot.com/2009/01/how-to-compress-php-and-other-text.html) 

但这基本上完全破坏了我网站的外观。


在我的 Functions.php 中尝试过,它似乎压缩了我的 html,但留下了一些未压缩的 js 和 css

if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
    add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));

【问题讨论】:

    标签: wordpress .htaccess gzip


    【解决方案1】:

    在我看来你已经用尽了你的选择。从上面看,主机似乎确实没有 mod_deflate 或 mod_gzip。所以我猜你只是运气不好。

    PHP 解决方案确实只适用于 HTML。所以坚持那个。 HTML 也是添加压缩的最佳位置,因为大多数情况下,CSS 和 JS 仅在第一页下载。

    您可以通过 PHP 脚本将请求重定向到 CSS 和 JS,并使用 PHP 进行压缩。但我不会去那里,因为您还必须实现 304 Not modified 并设置适当的过期标头。

    【讨论】:

    • 当别人告诉你应该放弃时,感觉要好得多;-) 谢谢!我将使用我的问题中列出的最终 sn-p。
    【解决方案2】:

    启用 gzip 压缩
    gzip压缩可以在php.ini中激活,代码如下:

    zlib.output_compression = On
    zlib.output_compression_level = 9
    allow_url_fopen = On
    

    【讨论】:

    • 我已经有一段时间没有解决这个问题了,但如果没记错的话,1&1 共享主机帐户不允许使用php.ini进行配置
    • 第二个,在 1&1 上为我工作。有没有人可以解释code here 的作用?
    • 这对我来说非常适合,对于 1&1 上的 WordPress
    【解决方案3】:

    我知道这个问题现在有点老了,但我找到了适合我的解决方案。

    将名为“php.ini”的文件添加到包含以下内容的根文件夹;

    zlib.output_compression = On
    zlib.output_compression_level = 9
    

    然后(这是您可能没想到的一点)将以下内容添加到您的 .htaccess 文件中;

    AddType x-mapp-php6 .html .htm .php
    

    是的,没错。我已经把php6 放在那里了。显然这将运行允许 gzip 压缩的最新稳定版本的 PHP(当前为 5.4)。这也将通过 PHP 解析器运行 .html.htm 文件,这意味着它们可以被压缩(未通过 PHP 解析器运行的文件将不会被压缩)。随意添加您希望通过 PHP 运行的任何其他扩展(例如.xml)。

    顺便说一句,如果你确实通过PHP运行.xml文件,记得设置标头声明它为xml文件,否则将无法正常工作。

    希望这会有所帮助!

    【讨论】:

      【解决方案4】:

      所以,过了一会儿,我想出了如何压缩具有 1&1 Webhosting 包的 html、css 和 js 文件。不支持放气!

      对于动态内容,您将 php.ini 添加到您网站的根目录。 php.ini 内容:

      zlib.output_compression =1
      zlib.output_compression_level =9
      

      当然你也可以选择其他的压缩级别,9 是最高的(并且会造成最高的服务器负载)。这将压缩您动态生成的 html 文件。

      要压缩静态文件(css、js 和图像...),您需要修改 .htaccess 文件。对于那个追加

      <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteBase /
      RewriteOptions Inherit
      ReWriteCond %{HTTP:accept-encoding} (gzip.*) 
      ReWriteCond %{REQUEST_FILENAME} !.+\.gz$ 
      RewriteCond %{REQUEST_FILENAME}.gz -f 
      RewriteRule (.+) $1.gz [QSA,L] 
      </IfModule>
      

      到您的 .htaccess 文件(您可以在网站的根目录中找到该文件 - 否则创建它)。 但压缩不会自动完成。所以你必须自己压缩文件!使用例如7-zip 并使用 .gz 压缩 js 和 css 文件 -> 结果应该是例如样式表.css.gz。 然后将文件上传到刚才压缩的文件所在的目录。

      现在应该可以了!

      PS:压缩并不总是有用的,尤其是当文件非常小时。所以检查压缩前后的差异。

      【讨论】:

      • 效果很好。您能否添加一种仅针对特定文件类型(例如 css 和 js)执行此操作的方法?
      【解决方案5】:

      您可以通过将此代码添加到您的 .htaccess 文件中来启用压缩:

      <IfModule mod_rewrite.c>
      
      AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml image/svg+xml image/x-icon text/css application/x-javascript application/javascript application/x-httpd-php application/x-httpd-fastphp application/x-httpd-eruby 
      
      </IfModule>
      

      【讨论】:

        【解决方案6】:

        为我工作,

        首先你必须复制所有目录中的 php.ini。 (1and1 在他们的常见问题解答中提供了一个脚本来促进这种操作) 与此内容:

        zlib.output_compression =1
        zlib.output_compression_level =9
        

        然后在 htaccess 中添加这个:

        <IfModule mod_gzip.c>
        mod_gzip_on Yes
        mod_gzip_item_exclude file \.(gz|zip|xsl)$
        mod_gzip_item_include mime ^text/html$
        mod_gzip_item_include mime ^text/plain$
        mod_gzip_item_include mime ^image/x-icon$
        mod_gzip_item_include mime ^httpd/unix-directory$
        mod_gzip_item_include mime ^text/javascript$
        mod_gzip_item_include mime ^application/javascript$
        mod_gzip_item_include mime ^application/x-javascript$
        mod_gzip_item_include mime ^text/x-js$
        mod_gzip_item_include mime ^text/ecmascript$
        mod_gzip_item_include mime ^application/ecmascript$
        mod_gzip_item_include mime ^text/vbscript$
        mod_gzip_item_include mime ^text/fluffscript$
        mod_gzip_item_include mime ^text/css$
        </IfModule>
        

        【讨论】:

          最近更新 更多