WP Super Cache protocol-aware caching patch
-
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)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘WP Super Cache protocol-aware caching patch’ is closed to new replies.