wordpress - 如何使 gzip 在 1and1 共​​享主机 WordPress 网站上工作

标签 wordpress .htaccess gzip

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);'));

最佳答案

所以,过了一段时间,我想出了如何压缩具有 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:压缩并不总是有用,尤其是当文件非常小时。因此检查压缩前后的差异。

关于wordpress - 如何使 gzip 在 1and1 共​​享主机 WordPress 网站上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420992/

相关文章:

php - WooCommerce - 在开始/结束日期之间的列表中获取事件订阅

php - WordPress 的 while 循环中的第一篇文章

html - 下一个元素垂直而不是水平

.htaccess - 访问新的 php 文件

.htaccess - .htaccess SEO 升级后重定向页面链接?

asp.net - 使用 MVC3 启用 gzip 压缩

javascript - 当我们有 gzip 时,为什么人们压缩 css 和 javascript?

bash - Gzip:将一组小文件(<64mb)合并为几个大文件(64mb或128mb)

php - 从数据库中获取随机数据,无需刷新页面

.htaccess - 在 codeigniter 中强制使用 SSL