Skip to content

Commit

Permalink
wpcom_vip_maybe_skip_old_slug_redirect(): Improve compatibility with …
Browse files Browse the repository at this point in the history
…local setups (#1381)

* Caching: Improve compatibility with local setups

Switch `$_SERVER[‘DOCUMENT_URI’]` to `$_SERVER[‘REQUEST_URI’] since that is the more popular key to be supported, particularly in local setups.

Amends logic by searching for `/http:` at position 0 instead of `http:` at position 1 (and likewise for “https:”).

Tidies documentation.

Fixes #976.

* Lint fix/style alignment

* Lint fix
  • Loading branch information
GaryJones committed Jun 2, 2020
1 parent b509527 commit 676207d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions vip-helpers/vip-caching.php
Expand Up @@ -707,18 +707,29 @@ function wpcom_vip_flush_wp_old_slug_redirect_cache( $post_id, $post, $post_befo
}

/**
* We're seeing an increase of urls that match this pattern: http://example.com/http://othersite.com/random_text
* These then cause really slow lookups inside of wp_old_slug_redirect, since wp_old_slug redirect does not match on full urls but rather former slugs it's safe to skip the lookup for these. (Most of the calls are from bad ad providers that generate random urls)
* Potentially skip redirect for old slugs.
*
* We're seeing an increase of URLs that match this pattern: http://example.com/http://othersite.com/random_text.
*
* These then cause really slow lookups inside of wp_old_slug_redirect. Since wp_old_slug redirect does not match
* on full URLs but rather former slugs, it's safe to skip the lookup for these. Most of the calls are from bad ad
* providers that generate random URLs.
*/
function wpcom_vip_maybe_skip_old_slug_redirect(){
function wpcom_vip_maybe_skip_old_slug_redirect() {
if ( ! is_404() ) {
return;
}

//We look to see if a malformed url (represented by 'http:' ) is right after the starting / in DOCUMENT_URI hence position 1
if ( is_404() && ( 1 === strpos( $_SERVER['DOCUMENT_URI'], 'http:' ) || 1 === strpos( $_SERVER['DOCUMENT_URI'], 'https:' ) ) ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return;
}

if ( 0 === strpos( $_SERVER['REQUEST_URI'], '/http:' ) || 0 === strpos( $_SERVER['REQUEST_URI'], '/https:' ) ) {
remove_action( 'template_redirect', 'wp_old_slug_redirect' );
remove_action( 'template_redirect', 'wpcom_vip_wp_old_slug_redirect', 8 );
}

}

function wpcom_vip_enable_maybe_skip_old_slug_redirect() {
add_action( 'template_redirect', 'wpcom_vip_maybe_skip_old_slug_redirect', 7 ); //Run this before wpcom_vip_wp_old_slug_redirect so we can also remove our caching helper
}
Expand Down

0 comments on commit 676207d

Please sign in to comment.