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.

Similar questions

Why would a completely normal loop give offset errors and maximum execution time errors?
I'm too angry about this problem to see it straight anymore, so I'm guaranteed to be overlooking something simple. Please help. Here's all my code has been stripped down to: Trying to run this code on the front page of my WordPress site has causes my CPU to spike, my fan to run hot, and one of two errors to surface. Either I get a long string of "O...
Handling Wordpress media files on EC2/S3 with auto scaling
I'm working on a WordPress deployment configuration on Amazon AWS. I have WordPress running on Apache on an Ubuntu EC2 instance. I'm using W3 Total Cache for caching and to serve user-uploaded media files from an S3 bucket. A load balancer distributes traffic to two EC2 instances with auto scaling to handle heavy loads. The problem is that user-upl...
How do I create a static page and redirect to a WordPress blog without moving files?
I have WordPress installed in the main directory. I want to create a static page so that when the person clicks "click here". It goes to the wordpress blog. I have tried this two ways thus far. Created a custom page in wordpress and then had it set as a static page. Problem when someone clicked the linked it redirected back to the splash page. Put ...
Wordpress redirect all .php files except core wordpress files
I've recently moved an old site from an old cms system to wordpress. The old site used .php file extensions for posts (ie. wesbitename.com/postname.php). When I moved the site to wordpress I tried to redirect all old links with .php extensions to extension-less posts. (wesbitename.com/postname.php redirects to wesbitename.com/postname). Here is the...
Wordpress slugs and 404 handling takes preference over folders in site root?
I have wordpress installed and permalink structure set to /%postname%/ In addition to WordPress folders, I also have a public_html/beta folder where I wanted to copy the same site and test some unreleased features. However, whenever I navigate to http://mysite.com/beta , I get the WordPress message saying "Sorry, but you're looking for something th...
How to use NGNIX directives to redirect all requests to files in a directory except for requests to files in a specific subdirectory?
I have a wordpress site where we want to control access to files in the media library. This means all files inside the /uploads/ directory need to be inaccessibile to non-logged-in users with the exception of a single subdirectory of files, /uploads/public/. I have a file called auth.php in the root of my site that does the actual permission checki...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .