How to stop WordPress redirecting 404s to guessed URLs

When a URL returns 404, WordPress looks for a published post whose slug begins with the last part of the requested address and redirects there. To stop it, add add_filter( 'do_redirect_guess_404_permalink', '__return_false' ); to your site. To keep it but require an exact slug match, use the strict_redirect_guess_404_permalink filter instead.

The feature is called permalink guessing. It rescues some broken links, and it also produces redirects nobody chose: a request for /app can land on a post called /apple-pie-recipe/.

Step by step

1. Recognise a guessed redirect

Signs that guessing is at work:

  • A URL you never created redirects to a post with a similar-looking slug.
  • A deleted page forwards to an unrelated post instead of showing your 404 page.
  • Search Console lists URLs under Page with redirect that match no rule in your redirect manager or server config.

Test a made-up prefix of a real slug:

bash
curl -sI https://example.com/espresso | grep -iE "^HTTP|^location"

If a post exists at /espresso-beans-guide/ and you get a 301 to it, that is the guess.

2. Understand how the guess is made

The function runs only after WordPress has decided the request is a 404. In its default loose mode it takes the requested slug and searches for a publicly viewable post whose post_name starts with it. If the URL contained a year, month, day or post type, those narrow the search. The first match wins, and the visitor is redirected to its permalink. In strict mode the slug must match exactly, which still helps when a post has moved to a different permalink structure or parent path but keeps its slug.

3. Decide: off, strict or as is

  • Leave it on if your site is small, slugs are distinct, and the occasional rescued link is welcome.
  • Make it strict if you want moved posts found by their exact slug but no prefix matching. This suits most content sites.
  • Turn it off if you manage redirects deliberately and want every unknown URL to return a real 404, which is easier to audit and avoids misleading redirects.

4. Add the filter

Put one of these in a small plugin or your child theme's functions.php. Both filters were added in WordPress 5.5.

Turn guessing off:

php
add_filter( 'do_redirect_guess_404_permalink', '__return_false' );

Keep it, exact matches only:

php
add_filter( 'strict_redirect_guess_404_permalink', '__return_true' );

A third filter, pre_redirect_guess_404_permalink, lets a developer supply their own target or return false for specific requests, for example to exempt one path prefix.

5. Retest

Run the curl command again. With guessing off, the made-up URL should answer HTTP/2 404. With strict mode, the prefix returns 404 while a request that uses the exact slug under the wrong path still redirects.

6. Replace the useful guesses with real redirects

Before guessing went off, some old links were being saved by it. Find them so they do not turn into dead ends. Switch on the 404 Monitor under Hydrogen SEO → 404 Monitor, give it a few days of traffic, and sort by hits. For each URL that real people or links are requesting, use the row's Redirect button and point it at the right page. That turns an accidental match into a rule you can see, edit and remove in Hydrogen SEO → Redirections.

What guessing does not affect

Two other core behaviours look similar and keep working with guessing off. The old-slug redirect, which forwards a renamed post's previous slug, runs separately. The canonical redirect, which fixes the trailing slash, the www prefix and paging in an otherwise valid URL, is also unaffected. Turning off guessing removes only the search for a similar post when nothing matches.

Why wrong guesses are a problem

A redirect to an unrelated post tells the visitor they arrived where they meant to go when they did not. For search engines, a removed URL that keeps redirecting somewhere loosely related can be treated as a soft 404, and it never clears from reports the way an honest 404 does. Guessing also hides mistakes: a broken internal link that happens to redirect looks fine until the matching post is renamed.

Common questions

Why does WordPress redirect a wrong URL to a different post?

Core's redirect_guess_404_permalink function looks for a post whose slug starts with the requested text and redirects to it. It is intended to rescue broken links.

Is it safe to disable 404 permalink guessing?

Yes. Unknown URLs then return a normal 404. Check your 404 log afterwards and add explicit redirects for any old links that were relying on it.

Does this stop the redirect after I rename a post?

No. Renamed posts are handled by a separate old-slug redirect, which keeps working.