【问题标题】:Browser caching for Wordpress site in shared hosting共享主机中 Wordpress 网站的浏览器缓存
【发布时间】:2015-06-22 05:18:18
【问题描述】:

我有一个在 Wordpress 4.1.1 下运行的网站。 This 是现场直播。我试图在 Google 的 PageSpeed Insights 中获得最高分。我在报告中遇到的问题之一是:

Leverage browser caching
Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

Leverage browser caching for the following cacheable resources:

http://connect.facebook.net/es_ES/all.js?ver=4.1.1 (20 minutes)
http://platform.twitter.com/…=O1TZPrUbWI8cZSAGqTb6v5PaU&v=1&ver=4.1.1 (30 minutes)
https://apis.google.com/js/api.js (30 minutes)
https://apis.google.com/js/client:plusone.js (30 minutes)
https://oauth.googleusercontent.com/…e:rpc:shindig.random:shindig.sha1.js?c=2 (60 minutes)
http://www.google-analytics.com/analytics.js (2 hours)
http://dynamotechnology.com/…ssets/min-css/ultimate.min.css?ver=3.9.4 (24 hours)
http://dynamotechnology.com/…/min-js/ultimate-params.min.js?ver=3.9.4 (24 hours)
http://dynamotechnology.com/…e_VC_Addons/assets/slick/ajax-loader.gif (24 hours)
http://dynamotechnology.com/…plus/css/comments-specific.css?ver=4.1.1 (24 hours)
http://dynamotechnology.com/…comments-plus/css/comments.css?ver=4.1.1 (24 hours)
http://dynamotechnology.com/…s/comments-plus/js/comments.js?ver=4.1.1 (24 hours)
http://dynamotechnology.com/…s/comments-plus/js/facebook.js?ver=4.1.1 (24 hours)

.....

对于 WP,这是我在主机 (Bluehost) 上的 .htaccess 文件:

# Use PHP54CGI as default
AddHandler fcgid54-script .php

# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP:Accept-Encoding} gzip
    RewriteRule .* - [E=W3TC_ENC:_gzip]
    RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]
    RewriteRule .* - [E=W3TC_PREVIEW:_preview]
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} =""
    RewriteCond %{REQUEST_URI} \/$
    RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
    RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
        AddOutputFilterByType DEFLATE text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/json
    <IfModule mod_mime.c>
        # DEFLATE by extension
        AddOutputFilter DEFLATE js css htm html xml
    </IfModule>
</IfModule>
# END W3TC Browser Cache

# START PageSpeed
<IfModule pagespeed_module>
   ModPagespeed off

   ModPagespeedEnableFilters rewrite_images,inline_images,recompress_images,convert_jpeg_to_progressive,recompress_jpeg,recompress_png,strip_image_meta_data,resize_images
   ModPagespeedEnableFilters rewrite_javascript,rewrite_css,make_google_analytics_async,remove_comments,collapse_whitespace

   ModPagespeedUseExperimentalJsMinifier on
</IfModule>
# END PageSpeed

为什么没有启用缓存?我还安装了 W3 Total Cache 插件。有什么建议吗?我做错了什么?如何缓存外部资源?那可以做到吗?怎么样?

【问题讨论】:

    标签: php wordpress .htaccess caching w3-total-cache


    【解决方案1】:

    尝试在 .htaccess 上添加过期规则:

    # BEGIN EXPIRES
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 10 days"
        ExpiresByType text/css "access plus 1 week"
        ExpiresByType text/plain "access plus 1 month"
        ExpiresByType image/gif "access plus 1 month"
        ExpiresByType image/png "access plus 1 month"
        ExpiresByType image/jpeg "access plus 1 month"
        ExpiresByType application/x-javascript "access plus 1 month"
        ExpiresByType application/javascript "access plus 1 week"
        ExpiresByType application/x-icon "access plus 1 year"
    </IfModule>
    # END EXPIRES
    

    对于外部资源(Google、Facebook 等),您无能为力。

    【讨论】:

    • 太棒了,行得通,你知道压缩或缩小 JS 和 CSS 的任何规则或任何 W3 Total Cache 配置吗?我也在共享主机中
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多