• emilycestmoi

    (@emilycestmoi)


    Hello,

    I would like to use wp super-cache in expert mode but I have found a problem which prevents me from doing so.

    I have a number of pages that are auto generated and use the wordpress internal add_rewrite_rule() function. For example, accessing https://www.emmywatch.com/movement/omega–560 on my site actually results in wordpress rewriting the url to https://www.emmywatch.com/db/index.php?page=2109&movslug=omega–560, which is processed by my site. The call I use to do this in wordpress is shown below:

    
    add_rewrite_rule('^' . $slug . '/movement/(.*)/?', 'index.php?page_id=' . $pageId . '&movslug=$matches[1]', 'top');
    

    Now of course I’d like these pages (/movement/omega–560, etc) to use the supercache so that I can serve them directly from nginx. But the problem is within the wp supercache plugin code, it is checking if $_GET is empty, if it is not empty supercache will never be used. Instead, you should be checking REQUEST_URI to see if there is a query string present, this will fix the issue and allow for others to use auto generated pages with add_rewrite_rule() like I am, otherwise these pages will never use the supercache.

    $dir = get_current_url_supercache_dir();
    $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '',  $home_url[ 'host' ]);
     if ( ! empty( $_GET ) || isset( $wp_super_cache_query[ 'is_feed' ] ) || ($super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
        wp_cache_debug( 'Supercache disabled: GET or feed detected or disabled by config.', 2 );
        $super_cache_enabled = false;
    }

    The above should use $_SERVER[‘REQUEST_URI’] (instead of checking !empty($_GET)) to check for the presence of a query string ‘?’ instead.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter emilycestmoi

    (@emilycestmoi)

    I couldn’t find a better plugin so I decided to fix this in wp supercache. This patch is applied to my live version on my site and is so far working fine and solves all problems while not changing any functionality:

    
    Index: wp-content/plugins/wp-super-cache/wp-cache-phase2.php
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    --- wp-content/plugins/wp-super-cache/wp-cache-phase2.php	(revision 3760)
    +++ wp-content/plugins/wp-super-cache/wp-cache-phase2.php	(revision )
    @@ -93,7 +93,7 @@
     		return false;
     	}
     
    -	if ( $wp_cache_no_cache_for_get && false == empty( $_GET ) ) {
    +	if ( $wp_cache_no_cache_for_get && wp_cache_is_get_query() ) {
     		wp_cache_debug( 'Non empty GET request. Caching disabled on settings page. ' . wpsc_dump_get_request(), 1 );
     		return false;
     	}
    @@ -145,8 +145,8 @@
     		if ( false == file_exists( $file ) ) {
     			wp_cache_debug( "No Super Cache file found for current URL: $file" );
     			return false;
    -		} elseif ( false == empty( $_GET ) ) {
    -			wp_cache_debug( 'GET array not empty. Cannot serve a supercache file. ' . wpsc_dump_get_request() );
    +		} elseif ( wp_cache_is_get_query() ) {
    +			wp_cache_debug( 'Non empty GET request. Cannot serve a supercache file. ' . wpsc_dump_get_request() );
     			return false;
     		} elseif ( wp_cache_get_cookies_values() != '' ) {
     			wp_cache_debug( 'Cookies found. Cannot serve a supercache file. ' . wp_cache_get_cookies_values() );
    @@ -1397,7 +1397,7 @@
     		return false;
     	}
     
    -	if ( ! empty( $_GET ) ) {
    +	if ( wp_cache_is_get_query() ) {
     		wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request. ' . wpsc_dump_get_request(), 5 );
     		$super_cache_enabled = false;
     	}
    @@ -1878,7 +1878,7 @@
     	if ( defined( 'DONOTCACHEPAGE' ) ) {
     		wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 );
     		$cache_this_page = false;
    -	} elseif ( $wp_cache_no_cache_for_get && ! empty( $_GET ) ) {
    +	} elseif ( $wp_cache_no_cache_for_get && wp_cache_is_get_query() ) {
     		wp_cache_debug( 'Non empty GET request. Caching disabled on settings page. ' . wpsc_dump_get_request(), 1 );
     		$cache_this_page = false;
     	} elseif ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) {
    @@ -2107,7 +2107,7 @@
     
     	$dir = get_current_url_supercache_dir();
     	$supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '',  $home_url[ 'host' ]);
    -	if ( ! empty( $_GET ) || isset( $wp_super_cache_query[ 'is_feed' ] ) || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
    +	if ( wp_cache_is_get_query() || isset( $wp_super_cache_query[ 'is_feed' ] ) || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
     		wp_cache_debug( 'Supercache disabled: GET or feed detected or disabled by config.', 2 );
     		$super_cache_enabled = false;
     	}
    @@ -2122,7 +2122,7 @@
     	}
     
     	if( $super_cache_enabled ) {
    -		if ( wp_cache_get_cookies_values() == '' && empty( $_GET ) ) {
    +		if ( wp_cache_get_cookies_values() == '' && !wp_cache_is_get_query() ) {
     			wp_cache_debug( 'Anonymous user detected. Only creating Supercache file.', 3 );
     			$supercacheonly = true;
     		}
    @@ -3301,6 +3301,17 @@
     		wp_cache_debug( 'GC Watcher: scheduled new gc cron.', 5 );
     		schedule_wp_gc();
     	}
    +}
    +
    +function wp_cache_is_get_query() {
    +	static $is_get_query = null;
    +
    +	if (null === $is_get_query) {
    +		$request_uri = parse_url($_SERVER['REQUEST_URI']);
    +		$is_get_query = $request_uri && !empty($request_uri['query']);
    +	}
    +
    +	return $is_get_query;
     }
     
     if ( ! function_exists( 'apache_request_headers' ) ) {
    
    Plugin Author Donncha O Caoimh (a11n)

    (@donncha)

    Interesting problem. Do you have this setting enabled?

    “Don’t cache pages with GET parameters.”

    Checking REQUEST_URI is probably a better way of doing this but I am sure that someone out there is relying on this behaviour and setting a $_GET variable to stop a page caching.

    Thread Starter emilycestmoi

    (@emilycestmoi)

    Hi! The default behavior is that if “Don’t cache pages with GET parameters” is unchecked then the plugin will use the fallback php file caching, not static file supercaching. The plugin is currently checking $_GET and preventing supercaching whether the “Don’t cache pages with GET parameters” is checked or not.

    The correct way to avoid supercaching is to check REQUEST_URI as is done in my patch above. I have been using this patch now for 1 month on my live site with no issues at all, and it continues to successfully use static file supercaching on all 500000+ pages that are auto generated.

    Thanks @emilycestmoi. I’ve prepared a PR based on your code. I’ll merge it next week unless you see anything wrong with it? I don’t use add_rewrite_rule() so I’d appreciate your eyes on it.

    https://github.com/Automattic/wp-super-cache/pull/813

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Check REQUEST_URI not $_GET when skipping supercache’ is closed to new replies.