A Gnome figurine

The other day I was working on one of my projects and I encountered an unexpected behavior: if you write the URL of a page in a WordPress site incorrectly, WordPress will try to guess what page you were trying to access and “fix” your request so that you get the proper page and not a 404 error.

Let’s look at an example, which is easier to understand.

A few weeks ago I wrote a blog post called Discover where you added errors in the code using Git Bisect. If you click on the link, you will see that its URL is https://neliosoftware.com/blog/git-bisect-to-find-faulty-commit/. Well, what would happen if you typed in a URL that’s close to this one, but still incorrect? Let’s try it: type https://neliosoftware.com/blog/git-bisect in your browser and you’ll see that WordPress performs a 301 redirection and makes sure you see my post:

Redirect an invalid URL to a valid URL
Redirect an invalid URL to a valid URL.

The thing is, this trick doesn’t always work. Instead of writing “git-bisect” (which is how the post’s slug begins), try something like “faulty-commit” (which is also part of the slug) and you’ll finally get the 404 error:

404 error with an incorrect URL
404 error with an incorrect URL.

So… yup, theses guesses work on very specific scenarios.

Is it good or bad that WordPress tries to fix a URL that does not exist instead of returning a 404? According to bug #16557 reported 9 years ago, there should be a filter to deactivate this behavior if the user wishes so. But although we have a patch for it, for some reason it never made it into core so… what if you wanted to deactivate this behavior?

Nelio A/B Testing

Native Tests for WordPress

Use your WordPress page editor to create variants and run powerful tests with just a few clicks. No coding skills required.

How to Avoid URL Auto-Correction

To solve this “problem” (assuming you think it is a problem), you have two options. Either you apply the solution proposed by Andrew Nacin (one of the leading WordPress developers) by adding this small function:

function remove_redirect_guess_404_permalink( $redirect_url ) {
  if ( is_404() ) {
    return false;
  }//end if
  return $redirect_url;
}//end remove_redirect_guess_404_permalink()
add_filter( 'redirect_canonical', 'remove_redirect_guess_404_permalink' );

in your WordPress (following the instructions we shared with you a few months ago) or, even simpler, you install the Disable URL Autocorrect Guessing plugin by Hauke Pribnow.

Featured image by David Brooke Martin on Unsplash.

2 responses to “DevTips – How to Stop WordPress From Guessing URLs”

  1. Tim Avatar
    Tim

    This no longer works on the latest WordPress version. Got an update for v6.n?

    1. David Aguilera Avatar

      It looks like the bug I mentioned in the post is finally fixed and merged into core. You may want to look into the new filters it provides: https://core.trac.wordpress.org/ticket/16557#comment:53

Leave a Reply

Your email address will not be published. Required fields are marked *

I have read and agree to the Nelio Software Privacy Policy

Your personal data will be located on SiteGround and will be treated by Nelio Software with the sole purpose of publishing this comment here. The legitimation is carried out through your express consent. Contact us to access, rectify, limit, or delete your data.