Disable URL Guessing in WordPress

Placing the following PHP code snippet below in your theme’s funtions.php file will disable a WordPress feature that attempts to automatically redirect a bad url to an actual post or page with a similar url.

Please note, if there are any broken links to your website or it’s pages that benefit from this feature it will now lead to a standard 404 error page.

/* Disable URL guessing - https://www.bloggersignal.com/stop-wordpress-from-guessing-urls */
add_filter('redirect_canonical', 'pz_no_redirect_404');

function pz_no_redirect_404($redirect_url)
{
    if (is_404()) {
        return false;
    }
    return $redirect_url;
}

The above was found in a post at https://www.bloggersignal.com/stop-wordpress-from-guessing-urls

Author: Joseph Dickson

Joseph Dickson is a front-end web developer with experience building higher education websites using ClassicPress, WordPress, and Drupal.

2 thoughts on “Disable URL Guessing in WordPress”

  1. Hi Joseph,

    Thank you for coming up this piece of code, but unfortunately it didn’t seem to work for me.

    WP 5.7.2

    Thank you

    1. Thanks Jean. I’ll need to revisit this when I have some free time. Looks like the website that inspired this snippet is no longer online. 🙁

Comments are closed.

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.