Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created April 17, 2020 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanknudtskov/fc372e90ef7f14582e63351158c7e02b to your computer and use it in GitHub Desktop.
Save yanknudtskov/fc372e90ef7f14582e63351158c7e02b to your computer and use it in GitHub Desktop.
In some cases autoptimize JS scripts will be cached in Google and return 404 results if the autoptimize cache has been cleared. To get around that problem this piece of code returns a HTTP 410 Gone for all those scripts that return a 404 because they have been cleared from the Autoptimize cache.
<?php
add_action( 'template_redirect', 'yanco_404_redirect_to_410_for_autoptimize' );
function yanco_404_redirect_to_410_for_autoptimize() {
if( is_404() ) {
$search = 'cache/autoptimize/js/autoptimize_';
if( strpos( $_SERVER['REQUEST_URI'], $search ) !== false ) {
wp_redirect( $_SERVER['REQUEST_URI'], 410 );
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment