skip to Main Content

could here someone know please?
Wordpress redirected to similar parent page if this exists, I need to add redirect to 404

For example I’ve created page
Website.com/parent1/child2
If I put
Website.com/parent2/child2
It’s redirected me to
Website.com/parent1/child2

So I need to 404 page, any ideas?

I’m also try use plugins

2

Answers


  1. <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://example.com/404-page");
    exit();
    ?>
    

    In this example, the code uses the header() function to send a 301 "Moved Permanently" HTTP status code and a new location header to redirect the user to the http://example.com/404-page URL.

    Note that this code should be placed at the top of your 404.php template, before any other HTML or PHP code. You should replace http://example.com/404-page with the URL of the custom 404 page you want to redirect to.

    Login or Signup to reply.
  2. you can try below code to redirect specific page url to 404 page.

    add_action( 'template_redirect', 'redirect_404_to_homepage' );
    
    function redirect_404_to_homepage(){
       if(is_404()):
            wp_safe_redirect( home_url('/') );
            exit;
        endif;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search