首页 > 解决方案 > 在 Wordpress 中删除 404 状态

问题描述

在 WordPress 中,我修改了我的 404 页面,因此该页面根据用户在 url 中键入的内容显示提要。因此,如果他键入mywebsite.com/orange并且我在网站上没有此页面,我的脚本将搜索orange并搜索结果,并且文章将显示在 404 页面上。

但是,我被 Adwords Campaign 禁止了,因为我的网站返回了许多 404 错误。如何在 404.php 页面中禁用 404 状态,以便 Google 不会认为那/orange/是 404。我希望 404.php 就像一个布局。(所以从技术上讲,我的网站不能有 404 )。

标签: phpwordpress

解决方案


您应该能够通过过滤设置的标头来做到这一点。这样的事情会阻止所有 404 标头被发送。

function theme_block_404($header, $code, $description, $protocol)
{
    if (intval($code) === 404) { //Is this a 404 header?
        $description = get_status_header_desc(200); //Get the default 200 description
        return "{$protocol} 200 {$description}"; //Return a 200 status header
    } else { //This isn't a 404 status
        return $header; //Don't change the header
    }
}
add_filter('status_header', 'theme_block_404');

请记住,这有点麻烦,因为它可以防止任何 404 错误被抛出。如果这适合您的情况,那就太好了,否则,您可能希望向此函数添加额外的逻辑,以使其在范围内更加具体。


推荐阅读