简体   繁体   English

Wordpress中如何将404 not found页面重定向到主页

[英]How to redirect 404 not found page to the home page in Wordpress

I have a 404 error page which I don't want to use.我有一个我不想使用的 404 错误页面。 Instead, I want to have the 404 page reload to my home page.相反,我想让 404 页面重新加载到我的主页。 I inserted the lines below in the beginning of the 404 php file:我在 404 php 文件的开头插入了以下行:

<?php
/* Redirect browser */

echo '<script type="text/javascript">
           window.location = "http://www.google.com/"
      </script>';

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

This works (it will load to my homepage. However, the new page will reload and a new tab page will open.这有效(它将加载到我的主页。但是,新页面将重新加载并打开一个新标签页。

I want to have the page reload and NOT to open a new tab.我想让页面重新加载,而不是打开一个新标签。

How can I do this?我怎样才能做到这一点?

Thank in advance预先感谢

You could use is_404 Docs function and hook it in the template_redirect Docs hook.您可以使用is_404 Docs function 并将其挂在template_redirect Docs挂钩中。 If it returns true you could use wp_safe_redirect Docs function to redirect to the home/front page.如果它返回 true,您可以使用wp_safe_redirect Docs function 重定向到主页/首页。

add_action('template_redirect', 'redirecting_404_to_home');

function redirecting_404_to_home()
{
    if (is_404()) 
    {
        wp_safe_redirect(site_url());
        exit();
    }
};

Code goes onto the functions.php file.代码进入functions.php文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM