【问题标题】:change wordpress 404 page 200 status code to 404将 wordpress 404 第 200 页状态码更改为 404
【发布时间】:2021-12-07 19:17:51
【问题描述】:

我想将 wordpress 默认的 404 页面状态码(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 )

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我怀疑将状态 200 到 404 页面是 WordPress 的默认行为,但我猜想在 404.php 顶部添加标题应该可以解决问题,

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

    因为我目前使用以下方式重定向我的 404:

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

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

    【讨论】:

    • 我在控制器中添加了status_header(404);。
    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 2013-01-09
    • 2011-08-01
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多