Serve apache 404 for missing assets rather then wp 404 template WP_Rewrites

This very helpful page is unfortunately kind of burried on the codex:

Basically mod_rewrite_rules hook will do the trick.

function faster_htaccess_rules( $rules ){
$faster_rules = <<<EOD
\n # BEGIN faster Apache 404 rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpg|jpeg|png|gif|ico|swf|bmp)$ - [R=404,L]
#Use instead for network sites
#RewriteRule \.(jpg|jpeg|png|gif|ico|swf|bmp)$ - [nocase,redirect=404,last]
# END faster Apache 404 rules\n\n
EOD;
return $faster_rules . $rules;
}
add_filter('mod_rewrite_rules', 'faster_htaccess_rules');

This prepend/apends the desired rules within the # Begin WordPress block, but outside of <IfModule mod_rewrite.c>. I had been exploring add_rewrite_rule() function, but it wont allow you to modify the RewriteRule flags.

Leave a Comment