WP Super Cache prune problem with SSL on

Recently, I have re-activated the WP Super Cache plugin to make my website responsing faster. But I notice that the post cache is not updated after editing. Further debugging shows the reason is that WP Super Cache does not deal with https related site properly.

1. circumstance of the problem (SSL on)

On my site ( https://cnzhx.net ), SSL (HTTPS) connection has been forced to use on wp-admin and wp-login pages:

  1. Setting up ssl on server;
  2. Add following 2 lines to wp-config.php
    define('FORCE_SSL_LOGIN', true);
    define('FORCE_SSL_ADMIN', true);

    just before line of

    /* That's all, stop editing! Happy blogging. */

After this, users could visit the site with or without https connection, but it’s always using https connection to visit the wp-admin area including post-edit.php page. And I think the WP Super Cache plugin is using https connection to check post update status, too.

2. The problem

Check the debug option on ( WP Super Cache Settings –> Debug ) and edit a post then update it. I get the following debug infomation:

01:04:49 /wp-admin/post.php wp_cache_post_edit: Clearing cache for post 7138 on post edit.
01:04:49 /wp-admin/post.php get_current_url_supercache_dir: warning! site_url (https://cnzhx.net) not found in permalink (https://cnzhx.net/blog/common-iptables-cli/).
01:04:49 /wp-admin/post.php get_current_url_supercache_dir: Removing SERVER_NAME (cnzhx.net) and https:// from permalink (https://cnzhx.net/blog/common-iptables-cli/). Is the url right?
01:04:49 /wp-admin/post.php supercache dir: /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.nethttp://blog/common-iptables-cli/
01:04:49 /wp-admin/post.php wp_cache_post_id_gc post_id: 7138 https://cnzhx.net/blog/common-iptables-cli/ clearing cache in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.nethttp://blog/common-iptables-cli/.
01:04:49 /wp-admin/post.php wp_cache_post_id_gc clearing cache in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.nethttp://blog/common-iptables-cli//page/.
01:04:49 /wp-admin/post.php wp_cache_post_id_gc clearing cache in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/.
01:04:49 /wp-admin/post.php Post change: deleting cache files in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/
.........( Homepage rebuilt correctly. ).........
01:04:49 /wp-admin/post.php wp_cache_post_edit: Already processed post 7138.
01:04:49 /wp-admin/post.php wp_cache_post_change: Already processed post 7138.

The plugin detects and starts a cache clearing and rebuilding. It assumes that the post url is starting with https (ssl connection), but when it is “browsing” the post, the actual url is starting with http.(line 2 and 3). So, the answer is “not right” when it asking “Is the url right?” in line 3. After that, it gets a wrong cache dir (line 4):

/{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.nethttp://blog/common-iptables-cli/

and old cache of the post is not cleared correctly (line 5).

Addtionally, there are 2 DIRs which are not actual existing and being used in this process (line 6 and 7):

/{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.nethttp://blog/common-iptables-cli//page/
/{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/

Highlighted with red font. I don’t understant the //page/ part. Whatever, it’s having nothing to do with the cache-not-rebuild-correctly problem.

Temporary Solutions

Here proposed 2 solutions. The first one is easy and not code related, while the second one needs to tamper codes of the WP Super Cache.

A. Easy one

Just don’t use ssl (https) connection on all the wp-admin pages and the problem goes away as it’s designed to be.

To do this, remove define('FORCE_SSL_ADMIN', true); and keep define('FORCE_SSL_LOGIN', true); mentioned in the 1st section.

B. Hard one

This problem is caused by function get_current_url_supercache_dir which is located in wp-cache-phase1.php, line 494 536. Related code snippet is:

if ( isset( $_SERVER[ 'HTTPS' ] ) )
    $protocol = ( 'on' == strtolower( $_SERVER[ 'HTTPS' ] ) ) ? 'https://' : 'http://';
else
    $protocol = 'http://';
if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$WPSC_HTTP_HOST}) and $protocol from permalink ($permalink). Is the url right?", 1 );
$uri = str_replace( $WPSC_HTTP_HOST, '', $permalink );
$uri = str_replace( $protocol, '', $uri );

So, I change the code snippet as following:

if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$WPSC_HTTP_HOST}) and $protocol from permalink ($permalink). Is the url right?", 1 );
$uri = str_replace( $WPSC_HTTP_HOST, '', $permalink );
$uri = str_replace( 'https://', '', $uri );
$uri = str_replace( 'http://', '', $uri );

It works! Now, the debug infomation looks like this:

03:51:00 /wp-admin/post.php wp_cache_post_edit: Clearing cache for post 7138 on post edit.
03:51:00 /wp-admin/post.php get_current_url_supercache_dir: warning! site_url (https://cnzhx.net) not found in permalink (https://cnzhx.net/blog/common-iptables-cli/).
03:51:00 /wp-admin/post.php get_current_url_supercache_dir: Removing SERVER_NAME (cnzhx.net) and  from permalink (https://cnzhx.net/blog/common-iptables-cli/). Is the url right?
03:51:00 /wp-admin/post.php supercache dir: /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/
03:51:00 /wp-admin/post.php wp_cache_post_id_gc post_id: 7138 https://cnzhx.net/blog/common-iptables-cli/ clearing cache in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/.
03:51:00 /wp-admin/post.php prune_super_cache: wp_cache_rebuild_or_delete( /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index-https.html )
03:51:00 /wp-admin/post.php rebuild_or_gc: rename to /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index-https.html.needs-rebuild
03:51:00 /wp-admin/post.php prune_super_cache: wp_cache_rebuild_or_delete( /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index.html )
03:51:00 /wp-admin/post.php rebuild_or_gc: rename to /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index.html.needs-rebuild
03:51:00 /wp-admin/post.php prune_super_cache: wp_cache_rebuild_or_delete( /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index-https.html.gz )
03:51:00 /wp-admin/post.php rebuild_or_gc: rename to /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index-https.html.gz.needs-rebuild
03:51:00 /wp-admin/post.php prune_super_cache: wp_cache_rebuild_or_delete( /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index.html.gz )
03:51:00 /wp-admin/post.php rebuild_or_gc: rename to /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli/index.html.gz.needs-rebuild
#
# After this line, the additonal / still exists before /page
#
03:51:00 /wp-admin/post.php wp_cache_post_id_gc clearing cache in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/blog/common-iptables-cli//page/.
03:51:00 /wp-admin/post.php wp_cache_post_id_gc clearing cache in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/.
03:51:00 /wp-admin/post.php prune_super_cache: deleted /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/3/index.html
03:51:00 /wp-admin/post.php prune_super_cache: deleted /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/3/index.html.gz
03:51:00 /wp-admin/post.php gc: deleted /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/3, forced delete
03:51:00 /wp-admin/post.php prune_super_cache: deleted /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/2/index.html
03:51:00 /wp-admin/post.php prune_super_cache: deleted /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/2/index.html.gz
03:51:00 /wp-admin/post.php gc: deleted /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net//page/2, forced delete
03:51:00 /wp-admin/post.php Post change: deleting cache files in /{HIDDEN_PATH}/wp-content/cache/supercache/cnzhx.net/
.........( Homepage rebuilt correctly. ).........
03:51:00 /wp-admin/post.php wp_cache_post_edit: Already processed post 7138.
03:51:00 /wp-admin/post.php wp_cache_post_change: Already processed post 7138.

But I don’t know if there are downsides with this tampering.

I report a bug on this in the support forum on wordpress.org. Hope that Donncha O Caoimh could fix it in the next version of WPSC officially.©

本文发表于水景一页。永久链接:<https://cnzhx.net/blog/wpsc-prune-problem-with-ssl-on/>。转载请保留此信息及相应链接。

时间过去太久,评论已关闭。
如果您有话要说,请到讨论区留言并给出此文章链接。
谢谢您的理解 :-)