Tanmay
Tanmay

Reputation: 25

How to retun 404 Not found for non-existing search result pages in WordPress instead of 404 Soft Error

I am facing problem with wordpress search. Suppose the url http://www.ampercent.com/search/page/2/ does not exist. But it shows 200 okay status instead of 404 not found. How to return 404 for such pages. This type of links are creating 404 soft error issue. This happens when you search an empty query in wordpress. e.g http://www.ampercent.com/?s=

Please help me some one.

Upvotes: 2

Views: 3312

Answers (2)

Helping Hands
Helping Hands

Reputation: 5396

Yes, solution available for this , Basically "404 soft" generates when some search done and no content found on page or sometime it may be very less content or something improper.

You can force this type of pages to redirect at 404 using following custom code.

Put following code in your functions.php :

 function force_404()
  {
     if ( have_posts() )
     {
         return FALSE;
     }

header( 'HTTP/1.0 404 Not Found' );
locate_template( '404.php', TRUE, TRUE );
$GLOBALS['wp_query']->is_404 = TRUE;
return TRUE;

}

Now call above function in top your files single.php & page.php as per below :

   if ( force_404() ) {
        return; //here it will stop any further process so soft 404 will not occur
     }

//your file other code continue 

Hope above will help to solve your issue.

Upvotes: 1

puttu
puttu

Reputation: 979

A simple work around for this is to just brute force the header call in the 404.php file.
For example:

<?php header('HTTP/1.0 404 Not Found'); ?>

<?php get_header(); ?>

Upvotes: 0

Related Questions