WPSurfer.com

How to Stop WordPress From Guessing URLs

Published on December 21, 2022 | Updated on July 3, 2024

The other day I was going through all the errors found on Google Search Console 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 redirect the visitor to the right page.

The problem with this functionality is that sometimes its guesses are wrong pretty much all the time.

Fortunately, there are ways to stop WordPress from auto-completing URLs


How to Stop WordPress From Guessing URLs

If you don’t mind installing one more plugin, you can prevent WordPress from guessing URLs using the Disable URL Autocorrect Guessing plugin.

If you don’t feel like installing a plugin that hasn’t been updated in two years, you might want to use a better approach.

This code will prevent WordPress from attempting to redirect to a “guessed” URL when a 404 error page is encountered.

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' );

You can insert that code to the Functions PHP file of your child theme, or you can use one of the free code snippet plugins

I recommend using mu-plugins so this the code if you want to take that route:

<?php
/**
 * Plugin Name: Disable 404 Redirect Guessing
 * Description: Prevent WordPress from guessing redirects for 404 pages.
 * Author: TicoLibre
 * Version: 1.0
 */

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' );
?>

Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.

WP SURFER

home

about

privacy

contact

© 2024 WP SURFER • Made with Love in Costa Rica