How do I skip wordpress's 404 handling and redirect all 404 errors for static files to 404.html?
How do I skip wordpress's 404 handling and redirect all 404 errors for static files to 404.html?
I read and it seems its not possible when using permalinks?
The objective is to reduce the server load for 404 errors by not loading php.
Solutions
Maybe a simple solution. Use the
conditional tag
is_404()
and create an redirect to your static file; include the code in the file
header.php
or
index.php
of the theme.
Here a example.
if ( is_404() ) {
wp_redirect( 'static.htm' );
exit;
}
Links
- http://codex.wordpress.org/Function_Reference/wp_redirect
- http://codex.wordpress.org/Function_Reference/is_404
.htaccess skip WordPress 404 error handling for static files .
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?)
RewriteCond %{REQUEST_FILENAME} \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ [NC]
RewriteRule .* - [L]
</IfModule>
Note: These rules were generated by the W3 Total Cache plugin*
Nginx skip WordPress 404 handling for static files.
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
if ($request_uri ~ "(robots\.txt|sitemap\.xml(\.gz)?)") {
break;
}
if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
return 404;
}
To extend on what Chris_O said....I would install W3 Total Cache and use the settings from that plugin to not cache static files. The plugin in itself is very useful and a must to speed up your site, especially with the latest update.
Also I recommend you have a look at Creating an Error 404 Page from Wordpress to see how to handle 404 for static files, 403 (forbidden), etc. Its a good read.