Home  >  Article  >  Backend Development  >  How to redirect 404 error page to homepage in WordPress using PHP

How to redirect 404 error page to homepage in WordPress using PHP

不言
不言Original
2019-04-10 09:53:353794browse

404 error means that the requested page does not exist on the server, if you have multiple 404 errors on your WordPress site, and you do not have a specific page that is 404. In this case, you can simply redirect all 404 requests via 301 to your website homepage.

How to redirect 404 error page to homepage in WordPress using PHP

To do this, you don’t need to install additional plug-ins on your site, a small PHP code can do it.

Redirect the 404 error page to the homepage

Go into your wordpress theme directory and edit the 404.php file in your wordpress. Let us add the following PHP script at the top of the file. The first line in the script will permanently redirect to 301.

<?php
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: ".get_bloginfo(&#39;url&#39;));
  exit();
?>

You can also replace .get-bloginfo("url") with your website domain name to avoid making an extra request to wordpress to get the domain name and make the redirect faster like below.

<?php
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.example.com");
  exit();
?>

This article has ended here. For more exciting content, you can pay attention to the PHP Video Tutorial column on the PHP Chinese website!

The above is the detailed content of How to redirect 404 error page to homepage in WordPress using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn