Support » Plugin: Redirection » Redirect the 404 urls to a search

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author John Godley

    (@johnny5)

    I wouldn’t advise redirecting a 404 page – a page that doesn’t exist should return a 404 code, not a 301. A better solution would be to put the search code into your themes 404.php page. There may be plugins to do this for you, or a change to your theme may be necessary.

    I did something kind of like this on my 404.php file:

    <?php
      $search_term = substr($_SERVER['REQUEST_URI'],1);
      $search_404 = str_replace('/','+',$search_term);
      $search_404 = str_replace('-','+',$search_404);
      $args=array('s' => $search_404, 'post_type' => 'post', 'paged' => $paged);
      $the_query = new WP_Query( $args );
     ?>
     <?php
      if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : 
          $the_query->the_post();
            get_post_type() );
        endwhile;
      endif;
    ?>

    It seems to work fine and give the user some help on what is happening. Pardon my coding as it’s mostly hacked from other sources 🙂

    Hope this helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect the 404 urls to a search’ is closed to new replies.