WordPress uses .htaccess file to handle all requests through the index.php file. This is a great feature because it enables us to use permalinks and all kinds of redirections. But it has one major pitfall. When a missing static file (404 error) is handled by WordPress (like a missing favicon.ico) this will cause your server to use more resources than required. In this post, I will show you a quick tip on how to skip WordPress handling of static files.


How to Skip WordPress 404 Handling for Static Files

As I mentioned at the beginning of the post, all magic is done via “.htaccess” file. In order to bypass WordPress for static files, follow the steps below:

  1. Open .htaccess file in your WordPress installation directory using an FTP application or Notepad++.
  2. Paste the following code at the beginning of the file:
    <IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !(robots\.txt|[a-z0-9_\-]*sitemap[a-z0-9_\-]*\.(xml|xsl|html)(\.gz)?) 
    RewriteCond %{REQUEST_URI} \.(css|htc|less|js|js2|js3|js4|html|htm|rtf|rtx|svg|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip)$ [NC] 
    RewriteRule .* - [L] 
    </IfModule>
  3. Save and upload the file.


How It Works?

When you add that code in your htaccess file, Apache will know that all requests ending with a static file extension are handled directly, while all other extensions will simply follow the next rule, which should be something like that one below:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Conclusion

WordPress is a highly dynamic content management system, and this is one of the top reasons everybody is switching to WordPress. But if you have a really huge traffic on your site, handling static files through WordPress may cause a lot of troubles. Using this simple tip, you can easily speed up your website at least by 5% and plus, you will use fewer CPUs on your WordPress hosting!

I hope you liked this post. Don’t forget to follow me on twitter for latest updates ;)

Best.