如何使gzip在1and1共享主机WordPress站点上工作

Zac*_*bey 8 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>  
Run Code Online (Sandbox Code Playgroud)

这会导致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>
Run Code Online (Sandbox Code Playgroud)

这(来自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>
Run Code Online (Sandbox Code Playgroud)

这个似乎什么也没做......

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript
Run Code Online (Sandbox Code Playgroud)

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

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

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


在我的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);'));
Run Code Online (Sandbox Code Playgroud)

小智 13

所以,过了一段时间,我想出了如何压缩具有1&1 Webhosting包的html,css和js文件.不支持Deflate!

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

zlib.output_compression =1
zlib.output_compression_level =9
Run Code Online (Sandbox Code Playgroud)

当然,您也可以选择另一个压缩级别,9是最高级别(并导致最高的服务器负载).这将压缩您动态生成的html文件.

要压缩静态文件(css,js和images ...),您需要修改.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>
Run Code Online (Sandbox Code Playgroud)

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

现在它应该工作!

PS:压缩并不总是有用,尤其是当文件非常小时.因此,请检查压缩前后的差异.


Ger*_*ben 7

它看起来像你已经用尽了你的选择.看看上面的内容似乎主机确实没有mod_deflate或mod_gzip.所以我想你只是运气不好.

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

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

  • 当其他人告诉你应该放弃时,感觉好多了;-)谢谢!我将使用我的问题中列出的最终片段. (2认同)

Lau*_*iuZ 6

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

zlib.output_compression = On
zlib.output_compression_level = 9
allow_url_fopen = On
Run Code Online (Sandbox Code Playgroud)