How to optimize your website by managing 404 errors of static files from the server

Should you optimize the resources of your website by having the server handle 404 errors of static files, instead of WordPress having to do it? Let’s see.

What does WordPress do with 404 errors?

Just so you understand what I’m talking about, when a WordPress website receives a request for a static resource, it queries the database and files to see if it finds it, and if not it dynamically generates a 404 error page, usually created by the active theme.

ideal 404 for dynamic content

For dynamic content it is great, as it is an opportunity to customize the 404 error page and redirect the user to other content.

What is the problem with 404 errors from static resources?

Now, if what the request was asking for was a static resource, a JPG file, CSS, JavaScript, font, etc., what we are doing is consuming WordPress resources and queries for requests that cannot be converted into legitimate, human traffic.

WordPress will still generate the error page, but usually without achieving any practical result for your website.

This is a very poor practice, as 404 error pages are created dynamically and cannot be properly optimized.

The solution to static resource 404 errors

The solution is that the web server, and not WordPress, manages the 404 errors of static resources that, normally, will never be converted into human traffic, and for that reason, it is not worth consuming WordPress resources with them.

To achieve this you just have to add a few lines to your server configuration file.

Apache

If your server is Apache add the following to the .htaccess file:

# Manage static 404 with Apache
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpg|jpeg|png|gif|webp|avif|svg|xls|doc|pdf|ico|eot|ttf|woff|woff2|otf|css|js)$ - [NC,R=404,L]Code language: PHP (php)

NGinX

If your server is Nginx then add these lines to the nginx.conf file:

# Manage static 404 with Nginx
location ~* \.(jpg|jpeg|png|gif|webp|avif|svg|xls|doc|pdf|ico|eot|ttf|woff|woff2|otf|css|js)$ {
try_files $uri =404 last;
}Code language: PHP (php)

Once these changes are made, from now on all 404 file errors in the code list will be handled by the server, without consuming resources from your WordPress installation or your hosting plan.

ideal 404 for static content

Meanwhile, 404 errors from dynamic content queries will continue to be managed from WordPress, to redirect and take advantage of that traffic.

ideal 404 for dynamic content

Who is this WPO trick for?

Any WordPress installation can benefit from this fantastic optimization technique, but it’s especially relevant for high-traffic sites, where each erroneous request – and there are usually many of them – consumes resources needed to serve legitimate content.

Another benefit associated with this change is that, by freeing your WordPress website from all that 404 error traffic, it can help you avoid crashes due to attacks that use 404 errors from static resources to take down your site, something more common than you imagine, and that security plugins like iThemes Security tries to avoid, but that with this trick you reinforce it.

How useful was this post?

Click on a smiley to rate it!

Average rating 4.5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Skip to content