DEV Community

Cover image for Change WordPress 404 redirect behaviour
Dave Kellam for Plank

Posted on

Change WordPress 404 redirect behaviour

When you visit a WordPress site url that doesn't exist, the default behaviour is to "guess" what you were trying to find. So, instead of landing on a 404 page, you'll possibly end up on a page that may or may not be what you were looking for.

This behaviour makes sense if the permalink structure (routes) have changed, and the post is still named the same. However, it can be a jarring experience for a user to end up on an unrelated page that happens to contain one of the words in the slug.

Two ways to modify the 404 behaviour

Turn off 404 guessing altogether:

add_filter( 'do_redirect_guess_404_permalink', '__return_false' );
Enter fullscreen mode Exit fullscreen mode

Use a strict redirect match (i.e. it will only redirect if it finds a post with the exact same slug):

add_filter( 'strict_redirect_guess_404_permalink', 'true' );
Enter fullscreen mode Exit fullscreen mode

Top comments (0)