Improving WordPress .htaccess – optimize WP’s 404 response

wordpress_404_page

WordPress has a 404.php template that’s generated for not found posts/pages. That’s fine, but what happens if post contains an image that’s deleted or moved from your blog or just path is set incorrect? It will output this 404.php template to the browser that’s actually doubles the traffic for a single page (for every not found object it will multiply traffic/load for every page)! Also, some bot’s request some strange urls never existed on your site that also slows down the server. To fix that I recommend to add elegant rules to WP’s .htaccess file:

RewriteCond %{REQUEST_FILENAME} !^.*\.png [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.jp?g [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.js [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.gif [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.css [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.js [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.xml [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.html [NC]

So updated .htaccess file will look like that:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^.*\.png [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.jp?g [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.js [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.gif [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.css [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.js [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.xml [NC]
RewriteCond %{REQUEST_FILENAME} !^.*\.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

5 thoughts on “Improving WordPress .htaccess – optimize WP’s 404 response

Leave a Reply

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