Home > Software engineering >  Wordpress add redirect to 404
Wordpress add redirect to 404

Time:02-03

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

CodePudding user response:

<?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.

CodePudding user response:

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;
}
  • Related