How To Cease WordPress From Guessing Redirects When It Believes It Will End in a 404 Error

News Author


If you happen to’ve been a longtime reader of Martech Zone, you’ve in all probability seen that there’s a ton of progress on cleansing up the positioning… particularly unhealthy inside hyperlinks, tons of redirects, elimination of articles to discontinued platforms, and updating of essential articles. Working nights and weekends, I’ve in all probability deleted over 1,000 articles after which edited over 1,000 extra… and my work isn’t achieved.

One other practical change that I’ve made to the positioning is incorporating referral hyperlinks to third-party platforms. If you happen to click on on a latest article, you’ll see that the hyperlink begins with https://martech.zone/refer/ after which brings you to the third celebration. That’s permitting me to do a few issues:

  • Observe all of my referrals to different firms in order that they’ll see the worth of sponsorship or present a referral hyperlink from my web site.
  • Chopping again on the variety of backlinks requests from lazy website positioning professionals as a result of they wish to use my web site as a backlink supply quite than present worth to my readers. My robots.txt file is up to date to dam the refer path from crawling.

One concern that I’m operating into with the variety of articles and inside redirects that I’ve on my web site is that WordPress’ default function the place it guesses a redirect is wreaking havoc. Core to that is that the redirect guess function doesn’t embody my redirects that are printed with Rank Math… however it’s typically overwriting them with irrelevant inside articles. I’m undecided if it’s a bug with their logic or simply an oversight, however it’s not optimum. Even worse is that if WordPress is offering these as 301 redirects to look engine crawlers.

How To Disable Redirect Guess Performance With WordPress

WordPress does supply the flexibility to take away this function altogether, however it’s not a visual possibility in your WordPress settings. As a substitute, you’ll must customise your theme file by including the next filter to capabilities.php:

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );

I don’t wish to dismiss the significance of this performance. In case you have a consumer who’s making an attempt to get to a web page in your web site and it ends in a 404 web page, that’s not an optimum expertise. Redirecting them with a related web page as an alternative is implausible. It’s simply that there are limitations the place it’s not optimum.

What I’ve seen is that I’ve a referral hyperlink in an article… regardless that it’s not a 404, WordPress guesses that it’s as a result of there’s no precise web page. So my readers get caught in a loop that by no means takes them to the vacation spot web page. It’s fairly irritating… so I wish to be sure that WordPress by no means guesses a redirect for paths which are accessible through redirects.

How To Disable Redirect Guesses With Particular Paths With WordPress

In consequence, I wrote a selected operate for my theme that ensures that WordPress by no means guesses in the case of some paths:

add_filter("do_redirect_guess_404_permalink", "modify_do_redirect_guess_404_permalink_defaults", 10, 1);
operate 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]";

    // Examine if the requested URL begins with the particular 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 particular paths
            $do_redirect_guess = false;
            break;
        }
    }

    // Return the modified $do_redirect_guess variable
    return $do_redirect_guess; 
}

This may very well be written, after all, for particular pages, posts, classes, or another use case the place you don’t need WordPress to guess the vacation spot web page after they can’t discover a particular URL.

Disclosure: Martech Zone is utilizing affiliate hyperlinks on this article.