Skip to content

Instantly share code, notes, and snippets.

@vmanthos
Created May 24, 2019 11:55
Show Gist options
  • Save vmanthos/e2952f7c41eb26ddc446b9800c4427fe to your computer and use it in GitHub Desktop.
Save vmanthos/e2952f7c41eb26ddc446b9800c4427fe to your computer and use it in GitHub Desktop.
Skip WordPress handling 404 on static files to avoid lack of performance.
<?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 = explode( '/', $path );
$abspath = explode( '/', ABSPATH );
$path = implode('/', array_diff( $path, $abspath ));
$extension = pathinfo( $_SERVER['REQUEST_URI'], PATHINFO_EXTENSION );
$extensions = [
'php' => 1,
'html' => 1,
'json' => 1,
'xml' => 1,
];
if ( strlen( $extension ) > 0 && ! isset( $extensions[$extension] ) && ! 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