Monday, March 27, 2023
HomeMobile MarketingHow To Cease WordPress From Guessing Redirects When It Believes It Will...

How To Cease WordPress From Guessing Redirects When It Believes It Will Lead to a 404 Error


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 dangerous inner hyperlinks, tons of redirects, removing of articles to discontinued platforms, and updating of important 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 carried out.

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 current article, you’ll see that the hyperlink begins with https://martech.zone/refer/ after which brings you to the third occasion. That’s permitting me to do a few issues:

  • Monitor all of my referrals to different firms in order that they will see the worth of sponsorship or present a referral hyperlink from my web site.
  • Chopping again on the variety of backlinks requests from lazy search engine marketing professionals as a result of they need to use my web site as a backlink supply fairly than present worth to my readers. My robots.txt file is up to date to dam the refer path from crawling.

One challenge that I’m operating into with the variety of articles and inner redirects that I’ve on my web site is that WordPress’ default characteristic the place it guesses a redirect is wreaking havoc. Core to that is that the redirect guess characteristic doesn’t embody my redirects that are printed with Rank Math… nevertheless it’s typically overwriting them with irrelevant inner articles. I’m undecided if it’s a bug with their logic or simply an oversight, nevertheless 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 characteristic altogether, nevertheless it’s not a visual possibility in your WordPress settings. As an alternative, you’ll need to customise your theme file by including the next filter to capabilities.php:

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );

I don’t need to dismiss the significance of this performance. You probably have a consumer who’s making an attempt to get to a web page in your web site and it leads to a 404 web page, that’s not an optimum expertise. Redirecting them with a related web page as a substitute is incredible. 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… although 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 need to ensure that WordPress by no means guesses a redirect for paths which can be obtainable through redirects.

How To Disable Redirect Guesses With Particular Paths With WordPress

In consequence, I wrote a selected perform for my theme that ensures that WordPress by no means guesses on the subject of some paths:

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

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

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

This might be written, after all, for particular pages, posts, classes, or some other 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments