I'm having a very strange effect on my website with occasional 404 errors.
First of all: This is not a normal problem with redirect rules or htaccess
. So please don't make comments like "check your rewrites". I know pretty well what I'm doing and can say with certainty that the following effect is not normal.
The 404 problem does not occur all the time and only occurs on pages. It doesn't occur here:
Yes, I use a lot of heavyweight plugins, but the server handles them very well and at a decent speed.
Normally everything works. But once in a while WordPress goes into this weird 404 mode and just stops displaying normal pages.
Initially the problem only appeared after I updated plugins. Not with every plugin, but tends to be the case with “heavy” plugins. Almost certainly this always happened after updating Yoast SEO.
Now after changing my theme from Twenty Thirteen to Astra Pro, the problem is also caused by other triggers. I can't say exactly what triggers it.
To display the pages again (at least until the next occurrence), the following helps:
As I said, it's not a problem with htaccess
or something similar, as everything normally works. The server is Nginx and my host Raidboxes handles this globally for tens of thousands of websites.
I found a possible clue here:
However, the memory limit is already 2048 MB and is not even close to being exhausted. Max script execution time is 1 minute and cannot be the problem (404 comes immediately).
Caching is regulated by my host, but has no impact on the effect. If I disable it, the problem still occurs.
Deactivating plugins cannot be a solution either. Firstly, I need the plugins installed (I know that I should be economical with this and try to avoid additional plugins at all costs, but what has to be, has to be). Also, the problem already occurred (after plugin updates) when I hadn't installed WooCommerce, wpForo, Learndash and a few others yet.
WordPress is always in the latest version (it has been for years), and the PHP version is always as up to date as my host allows (currently 8.2).
Because I now had an urgent need for action (it's just annoying to have to re-save the permalinks every 15 minutes to make the pages available again, not to mention the poor user experience), I added a workaround to my Astra child theme's functions.php
:
add_action('template_redirect', 'hkp_404_workaround');
function hkp_404_workaround ()
{
if (is_404())
{
if (!isset($_GET['redirect404']))
{
$url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$redirect_to = $url . (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'redirect404=1';
//flush_rewrite_rules(); // no effect
delete_option('rewrite_rules'); // works great
wp_redirect($redirect_to);
exit;
}
}
}
Here a hook is added to the output of the 404 page, deleting the rewrite_rules
(forcing WordPress to recreate them) and then redirecting to the same URL that was called. The site is then back – works very well as a temporary workaround! An additional parameter ensures that in the event of a real 404 error, the whole thing doesn't end up in an endless redirect loop. I removed additional logging from the above code for greater clarity.
As I said, it works for now, but I would like to find the root cause and fix it.
I would be grateful for any clues as to how I could find the cause.
Unfortunately, I can't do any debug output or anything similar in WordPress Core because my host doesn't offer write permissions (Core directories are only integrated via symlinks). However, I could add something to wp_config.php
.