Home > Back-end >  How to stop auto-guessing of urls and redirect all Invalid urls to 404 in wordpress
How to stop auto-guessing of urls and redirect all Invalid urls to 404 in wordpress

Time:06-04

I have a site example.com. When I try to load a URL example.com\a it will automatically redirect to example.com\about-me page and the same for other letters as well. Is there a way to redirect all the non existing URLs to 404?

CodePudding user response:

I think I found the solution!

Adding this to the function file solves the problem I am facing it's a new feature in WordPress 5.5 version to auto-redirect to the nearest match URL if an incorrect URL is entered.

add_filter('do_redirect_guess_404_permalink','stop_redirect_guess');
function stop_redirect_guess() {
    return false;
}

For older version of WordPress

add_filter( 'redirect_canonical','stop_redirect_guess' );
function stop_redirect_guess( $url ) {
    if ( is_404() ) {
        return false;
    }
        return $url;
}

And just adding the below code to .htaccess file will redirect you to a 404 custom page you have made or your theme has.

ErrorDocument 404 /404.php 

Or you can add a link directly like(worked for me)

ErrorDocument 404 example.com/404

CodePudding user response:

Wordpress automaticly handles invalid url requests. If invalid urls not redirect to 404 page you should check your .htaccess file. If Wordpress can redirect default 404 page, you can edit your existing 404.php file or create new one. Here is redirect code for your 404.php file:

header("Location: your about-me page url");
  • Related