How to Stop WordPress From Guessing URLs

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 autocompleting 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. Instead, it will allow the 404 error page to be displayed.

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 your Functions.php file via a custom plugin, a child theme, or a code snippet plugin

More Code Snippets for WordPress

You might want to check out some of these posts before you leave

  1. How to Apply CSS Conditionally
  2. GeneratePress Hooks: Simple Guide
  3. Managing your Site Disavow File
  4. Removing and Redirecting Query Strings
  5. Adding a Class to Images, Videos, and Iframes
  6. How To Remove the Category From the WordPress URL
  7. How to Delay The Execution of JavaScript without a plugin
Manuel Campos, English Professor

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