How To Stop WordPress From Guessing Redirects When It Believes It Will Result in a 404 Error

If you’re a longtime reader of Martech Zone, you’ve probably noticed much progress on cleaning up the site. I’ve been correcting incorrect internal links, adding many redirects, removing articles to discontinued platforms, and updating critical articles. Working nights and weekends, I’ve probably deleted over 1,000 articles and edited over 1,000 more… and my work isn’t done.
Another functional change I’ve made to the site is incorporating referral links to third-party platforms. If you click on a recent article, you’ll see that the link starts with https://martech.zone/refer/ and then brings you to the third party. That’s allowing me to do a couple of things:
- Track all of my referrals to other companies so that they can see the value of sponsorship or provide a referral link from my site.
- Cutting back on the number of backlinks requests from lazy SEO professionals because they want to use my site as a backlink source rather than provide value to my readers. My robots.txt file is updated to block the refer path from crawling.
One issue I’m running into with the number of articles and internal redirects I have on my site is that WordPress’ default feature, where it guesses a redirect is wreaking havoc. Core to this is that the redirect guess feature doesn’t include my redirects published with Rank Math… but it’s often overwriting them with irrelevant internal articles. I’m unsure if it’s a bug with their logic or just an oversight, but it’s not optimal. Even worse is if WordPress provides these as 301 redirects to search engine crawlers.
How To Disable Redirect Guess Functionality With WordPress
WordPress does offer the ability to remove this feature altogether, but it’s not a visible option in your WordPress settings. Instead, you’ll have to customize your theme file by adding the following filter to functions.php:
add_filter( 'do_redirect_guess_404_permalink', '__return_false' );
I don’t want to dismiss the importance of this functionality. If you have a user trying to get to a page on your site that results in a 404 page, that’s not an optimal experience. Redirecting them with a relevant page instead is fantastic. It’s just that there are limitations where it’s not optimal.
I’ve noticed that I have a referral link in an article… even though it’s not a 404, WordPress guesses that it is because there’s no actual page. So, my readers get caught in a loop that never takes them to the destination page. It’s pretty frustrating… so I want to make sure that WordPress never guesses a redirect for paths that are available via redirects.
How To Disable Redirect Guesses With Specific Paths With WordPress
As a result, I wrote a specific function for my theme that ensures that WordPress never guesses when it comes to some paths:
add_filter("do_redirect_guess_404_permalink", "modify_do_redirect_guess_404_permalink_defaults", 10, 1);
function modify_do_redirect_guess_404_permalink_defaults($do_redirect_guess) {
// Get the requested URL
$requested_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// Check if the requested URL starts with the specific paths
$specific_paths = array('https://martech.zone/refer/', 'https://martech.zone/acronym/');
foreach ($specific_paths as $specific_path) {
if (substr($requested_url, 0, strlen($specific_path)) === $specific_path) {
// Disable 404 guessing for the specific paths
$do_redirect_guess = false;
break;
}
}
// Return the modified $do_redirect_guess variable
return $do_redirect_guess;
}
This could be written for specific pages, posts, categories, or any other use case where you don’t want WordPress to guess the destination page when they can’t find a specific URL.