【问题标题】:How to retun 404 Not found for non-existing search result pages in WordPress instead of 404 Soft Error如何在 WordPress 中为不存在的搜索结果页面返回 404 Not found 而不是 404 Soft Error
【发布时间】:2015-02-21 07:36:27
【问题描述】:

我遇到了 wordpress 搜索的问题。假设 url http://www.ampercent.com/search/page/2/ 不存在。但它显示 200 正常状态而不是 404 未找到。如何为此类页面返回 404。这种类型的链接会产生 404 软错误问题。当您在 wordpress 中搜索空查询时会发生这种情况。例如http://www.ampercent.com/?s=

请帮我一个。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    是的,可用的解决方案,基本上“404 soft”会在某些搜索完成并且在页面上找不到内容时生成,或者有时内容可能非常少或不合适。

    您可以使用以下自定义代码强制此类页面在 404 处重定向。

    在你的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;
    
    }
    

    现在在您的文件 single.php 和 page.php 顶部调用上述函数,如下所示:

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

    希望以上内容能帮助您解决问题。

    【讨论】:

    • 它正在工作,但是当我使用 Genesis 时,我正在使用以下代码: function force_404() { if ( have_posts() ) { return FALSE; } header('HTTP/1.0 404 Not Found');定位模板('404.php',真,真); $GLOBALS['wp_query']->is_404 = TRUE;返回真; } add_action('get_header','oxh_soft_404');函数 oxh_soft_404(){ if(is_search()){ if ( force_404() ) { return; //在这里它将停止任何进一步的过程,因此不会发生软 404 } } } 。但主题正在被打破。 imgur.com/46zulO1
    • 您遇到了什么具体问题?
    【解决方案2】:

    解决这个问题的一个简单方法是暴力破解 404.php 文件中的标头调用。
    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2010-09-25
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      • 2016-02-10
      • 2017-07-15
      相关资源
      最近更新 更多