将wordpress 404页面200状态代码更改为404

时间:2015-08-13 09:09:40

标签: php wordpress

我想将wordpress默认4040页面状态代码(200 OK)更改为404。 我试图像这样,但它仍然返回404页面,状态为200 Ok。

add_action('template_redirect', '_custom_redirect');
function _custom_redirect() {
        global $wp_query;
        $wp_query->set_404();
        status_header(404);
        include( get_query_template( '404' ) );
        exit;
}

我需要为Google做这件事。有很多旧的网址,谷歌仍在索引,但没有现有和返回404未找到。我想如果我将返回状态404,它将停止索引(根据此video

1 个答案:

答案 0 :(得分:3)

我怀疑将状态200提供给404页是WordPress的默认行为,但是会猜测在404.php之上添加标题应该可以解决问题,

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

因为我目前将我的404重定向到:

<?php header("Location: ../"); ?>

这可能是一种解决方法。 GL吧!

相关问题