title: Set web server handle 404 errors for some file types using .htaccess (ErrorDocument 404 + rewrite rules)
body: <p>It's my first question in this great community (please tell me if I'm doing anything wrong). I learned a lot of from your answers in the past 5-6 years and now I have very specific question, which I couldn't find on the Internet (is this possible?)...</p>
  
  <p>The goal is to set custom ErrorDocument 404, that handles not existing <em>chosen static files</em> like: <em>/notfound.jpg</em> and in a subdirectory (for any directory): <em>/dir/notfound.jpg</em>, for sites running under Wordpress with pretty permalinks enabled. It will avoid high load on the server when some chosen static files are missing or linked incorrectly!</p>
  
  <p>I'm using a slightly modified code from the W3 Total cache plugin in my .htaccess file:</p>
  
  <pre><code>ErrorDocument 404 /_code_404.html
  
  # BEGIN (modified) W3TC Skip 404 error handling by WordPress for static files
  &lt;IfModule mod_rewrite.c&gt;
  RewriteEngine On
  RewriteCond %{REQUEST_URI} !(robots\.txt|sitemap\.xml(\.gz)?)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} \.(css|js|bmp|gif|ico|jpg|jpeg|jpe|png|swf)$ [NC]
  RewriteRule .* - [L]
  &lt;/IfModule&gt;
  # END (modified) W3TC Skip 404 error handling by WordPress for static files
  
  # BEGIN WordPress
  &lt;IfModule mod_rewrite.c&gt;
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
  &lt;/IfModule&gt;
  # END WordPress
  </code></pre>
  
  <p>I'm trying it on 2 different servers with Cpanel, on shared plans, both have Wordpress with pretty permalinks and the result is the same:</p>
  
  <ol>
  <li><p>Works just as expected for requests like <em><a href="http://mydomain.com/notfound.jpg" rel="nofollow">http://mydomain.com/notfound.jpg</a></em></p></li>
  <li><p>Doesn't work for not found file in a subdirectory like this <em><a href="http://mydomain.com/dir/notfound.jpg" rel="nofollow">http://mydomain.com/dir/notfound.jpg</a></em>. In this case the WP handles the request and shows 404 page, which I want to avoid for certain file types, i.e. for static files like: css, js, jpg and a few more...</p></li>
  </ol>
  
  <p>Any answers and comments are welcome, thanks! I can try any suggestions you may have (even untested).</p>
  
  <p><strong>EDIT 1:</strong> A note to each other trying to get it to work (see accepted answer + the note below):</p>
  
  <p><strong>It seems you must (or depend on server config) set custom error 404 page, otherwise the rewrite which excludes some chosen file types will not work and as a result the WP will process the request!</strong> (it seems not much people note this, or it could be related to the server config)</p>
  
  <p>I realized that, because I was trying the proposed solution even before I write this question, but without the "ErrorDocument 404" line and it didn't work.</p>
  
  <p><strong>EDIT 2:</strong> Tests and another note:</p>
  
  <ol>
  <li><p>During the tests I tested the URLs with query string: /12345.css?v=5 and /dir4/12345.css?v=3&amp;t=66 [It works fine, because REQUEST_URI excludes query string!]</p></li>
  <li><p>Very important thing to note is that the server usually should return HTTP code 404 for missing files (otherwise search engines can hurt your rankings). If you set the full domain name in: ErrorDocument 404 http: //mydomain.com/_code_404.html, then the the "client will not receive the original error status code, but instead will receive a redirect status code". (reference - httpd.apache.org/docs/2.0/mod/core.html#errordocument)</p></li>
  </ol>