skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search