404’s are part of every website. A while ago we compiled a list of some pretty cool WordPress 404 designs. When coding a one page site, you might not have the time to create a custom 404 page. In which case, you might as well redirect the 404 page to your site’s homepage. In this article, we will show you how to redirect 404 page to home page in WordPress.
404是每个网站的一部分。 不久前,我们编制了一些非常酷的WordPress 404设计列表。 对一页网站进行编码时,您可能没有时间创建自定义404页面。 在这种情况下,您最好将404页面重定向到您网站的首页。 在本文中,我们将向您展示如何在WordPress中将404页面重定向到主页。
All you have to do is open your 404.php file in your theme’s folder. If it doesn’t exist, then create a blank php file. Paste the following code in there:
您所要做的就是在主题文件夹中打开404.php文件。 如果不存在,则创建一个空白的php文件。 在其中粘贴以下代码:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".get_bloginfo('url'));
exit();
?>
That’s all. Now when a user hits a 404 page will be redirected to the homepage.
就这样。 现在,当用户点击404页面时,将被重定向到首页。
Note: This should be used in very specific cases. In most other cases (i.e blogs, portfolios etc), you should track your 404 pages and redirect them appropriately.
注意:应在非常特殊的情况下使用。 在大多数其他情况下(例如,博客,投资组合等),您应该跟踪404页面并适当地重定向它们 。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-redirect-your-404-page-to-the-home-page-in-wordpress/