Tuesday, March 28, 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


For those who’ve been a longtime reader of Martech Zone, you’ve most likely observed that there’s a ton of progress on cleansing up the location… particularly unhealthy inner hyperlinks, tons of redirects, elimination of articles to discontinued platforms, and updating of important articles. Working nights and weekends, I’ve most likely deleted over 1,000 articles after which edited over 1,000 extra… and my work isn’t executed.

One other purposeful change that I’ve made to the location is incorporating referral hyperlinks to third-party platforms. For those who 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:

  • Monitor all of my referrals to different corporations in order that they’ll see the worth of sponsorship or present a referral hyperlink from my website.
  • Chopping again on the variety of backlinks requests from lazy search engine optimization professionals as a result of they wish to use my website 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 situation that I’m operating into with the variety of articles and inner redirects that I’ve on my website 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 embrace my redirects that are printed with Rank Math… however it’s typically overwriting them with irrelevant inner 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 go looking engine crawlers.

How To Disable Redirect Guess Performance With WordPress

WordPress does provide the flexibility to take away this characteristic altogether, however it’s not a visual possibility in your WordPress settings. As an alternative, you’ll should customise your theme file by including the next filter to features.php:

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );

I don’t wish to dismiss the significance of this performance. You probably have a person who’s making an attempt to get to a web page in your website 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 observed is that I’ve a referral hyperlink in an article… though 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 guarantee that WordPress by no means guesses a redirect for paths which can be accessible by way of 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 relation to 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]";

    // Test 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

- Advertisment -
Google search engine

Most Popular

Recent Comments