• I collaborate in managing a large multisite installation. We are using wp-super-cache, but we ran into an issue: since we allow our users to browse the blogs both in http and in https, we had issues with the fact that the cache does not divide cached contents for the two cases. We made a simple patch to allow for caching the two protocols. The separate caching allows for https users to be served pages with all links in https, so that they do not get any security warning.

    You may consider this patch as a POC, since I’m relatively new to wp-super-cache, but I think it does his job.

    diff -rupN wp-super-cache.orig/wp-cache-phase1.php wp-super-cache/wp-cache-phase1.php
    --- wp-super-cache.orig/wp-cache-phase1.php	2011-03-16 00:07:38.000000000 +0100
    +++ wp-super-cache/wp-cache-phase1.php	2011-03-16 08:44:09.339082990 +0100
    @@ -459,7 +459,10 @@ function get_current_url_supercache_dir(
     	}
     	$uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $uri ) ) ) );
     	$uri = str_replace( '\\', '', $uri );
    -	$dir = preg_replace( '/:.*$/', '',  $_SERVER["HTTP_HOST"] ) . $uri; // To avoid XSS attacks
    +	//Add support for https and http caching
    +	$is_https = ('on' ==  strtolower($_SERVER['HTTPS'])  || 'https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])); //Also supports https requests coming from an nginx reverse proxy
    +	$schema_dir = $is_https ? '/https' : '/http';
    +	$dir = preg_replace( '/:.*$/', '',  $_SERVER["HTTP_HOST"] ) . $schema_dir . $uri; // To avoid XSS attacks
     	if ( function_exists( "apply_filters" ) )
     		$dir = apply_filters( 'supercache_dir', $dir );
     	$dir = $cache_path . 'supercache/' . $dir . '/';
    diff -rupN wp-super-cache.orig/wp-cache.php wp-super-cache/wp-cache.php
    --- wp-super-cache.orig/wp-cache.php	2011-03-16 00:07:38.000000000 +0100
    +++ wp-super-cache/wp-cache.php	2011-03-16 09:00:44.239082997 +0100
    @@ -2450,14 +2450,24 @@ function wpsc_get_htaccess_info() {
     	$rules .= "RewriteBase $home_root\n"; // props Chris Messina
     	$charset = get_option('blog_charset') == '' ? 'UTF-8' : get_option('blog_charset');
     	$rules .= "AddDefaultCharset {$charset}\n";
    +	$rules .= "#If you serve pages from behind a proxy you may want to change RewriteCond %{HTTPS} on to something more sensible\n";
     	$rules .= "CONDITION_RULES";
     	$rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
    -	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz -f\n";
    -	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html.gz\" [L]\n\n";
    +	$rules .= "RewriteCond %{HTTPS} on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html.gz -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html.gz\" [L]\n\n";
    +	$rules .= "RewriteCond %{HTTP:Accept-Encoding} gzip\n";
    +	$rules .= "RewriteCond %{HTTPS} !on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html.gz -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html.gz\" [L]\n\n";
    
     	$rules .= "CONDITION_RULES";
    -	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html -f\n";
    -	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}{$home_root}$1/index.html\" [L]\n";
    +	$rules .= "RewriteCond %{HTTPS} on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/https{$home_root}$1/index.html\" [L]\n";
    +	$rules .= "RewriteCond %{HTTPS} !on\n";
    +	$rules .= "RewriteCond {$apache_root}{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html -f\n";
    +	$rules .= "RewriteRule ^(.*) \"{$inst_root}cache/supercache/%{HTTP_HOST}/http{$home_root}$1/index.html\" [L]\n";
     	$rules .= "</IfModule>\n";
     	$rules = apply_filters( 'supercacherewriterules', $rules );

    Hope this helps.

Viewing 10 replies - 1 through 10 (of 10 total)
  • It looks good! Hopefully I’ll get that in and working before 1.0 (the next release) if you can test it?

    Actually, those rewrite rules won’t be correct. The CONDITION_RULES string needs to be in both chunks of rules. Fixing it while merging!

    Checked that patch (and a slight mod fixing the above problem) into trunk. Should be in the development version in about 15 minutes.

    Happy!! 🙂

    I love it when a plan comes together.

    Hmm, I just realised this is incomplete. It doesn’t handle deleting cache files at all as the supercache directory is referenced in other places too.

    Thread Starter 0blivian

    (@0blivian)

    Sorry for the incomplete patch to the rewrite rules, I wrote it afterwards as I changed the rewrite rules by hand while developing.

    As of deleting cache files, my cache files do get pruned, but I suspect I have some issues with pruning in my installation (it happens way too often, I have to look into that). If you point me to where I should correct the deletion of the cache files, I’d be happy to help with a patch.

    Also, I’ve seen that the pruning function is called recursively. If I understood its working correctly, this can be a problem on large installations (with thousands of blogs) as you may risk encountering the max recursion limit of php (100 by default) when pruning directories recursively.

    I think I’ve patched the final bit of code and tested it by leaving a comment on one of my blogs running it. Can you give the development version a go shortly?

    Thread Starter 0blivian

    (@0blivian)

    this is great! we’ll test the development version asap. We have quite a large userbase, so we’ll test it both off and online. I’ll let you know if it works as intended.

    Any luck with this?

    I tested it here and it seems to work fine for me.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘WP Super Cache protocol-aware caching patch’ is closed to new replies.