WordPress slow 404 at Apache

After installing WordPress, you should definitely do a basic performance check. You should at least watch the logs and run Htop (or just Top). When you look at the access_log, you can find many 404 errors that need to be fixed.

After you’ve fixed 404 errors, you need to make sure that if they reappear, they won’t take up a lot of server resources.

Let’s take Apache as an example. With the default WordPress installation, there is no check whether a static resource file (image or script) exists or not. If the file doesn’t exist, the default WordPress 404 page is rendered. But 404 pages are not cached, and if you have a lot of such 404 responses on static, the server will waste a lot of resources.

Therefore, you need to add a configuration that will check whether the file exists or not, and if it does not exist, it will quickly give a 404 response at the Apache level. Each time you install WordPress on Apache, add the following to the virtual host level in the configuration:

<IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond /var/www/html%{REQUEST_FILENAME} !-f
      RewriteRule \.(css|js|ico|gif|jpg|jpeg|png|swf|flv|pdf|map|mov|svg|mp4|mp3|zip|eot|ttf|woff|woff2|ttc|otf)$ - [nocase,redirect=404,last]

Pay attention to RewriteCond. There /var/www/html is the directory where your site is located. Replace with your own if the site is located elsewhere.

This rule checks if the file does not exist, then there is an instant 404 response without any resource consumption. This way you can significantly improve the performance of WordPress, the site will load faster.

Leave a Reply

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