Skip to content

Instantly share code, notes, and snippets.

@GeekPress
Last active February 4, 2020 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeekPress/61841168058b1def489eefa530e45d9a to your computer and use it in GitHub Desktop.
Save GeekPress/61841168058b1def489eefa530e45d9a to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Speed Up 404 Static File
* Description: Skip WordPress handling 404 on static files to avoid lack of performance.
* Author: GeekPress
* Licence: GPLv2 or later
* */
$path = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
$path = trim( $path, '/' );
$extension = pathinfo( $path, PATHINFO_EXTENSION );
$basename = pathinfo( $path, PATHINFO_BASENAME );
$allowed_extensions = [
'js' => 1,
'css' => 1,
'ico' => 1,
'png' => 1,
'jpg' => 1,
'jpeg' => 1,
'gif' => 1,
'webp' => 1,
'svg' => 1,
'ogg' => 1,
'mp4' => 1,
'webm' => 1,
'ttf' => 1,
'otf' => 1,
'ttf' => 1,
'woff' => 1,
'woff2' => 1,
];
// Don't try to optimize these files.
$excluded_files = [
'superpwa-sw.js' => 1,
];
if ( strlen( $extension ) > 0 && isset( $allowed_extensions[$extension] ) && ! $excluded_files[$basename] && ! file_exists( ABSPATH . $path ) ) {
status_header( 404 );
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment