Home >Backend Development >PHP Tutorial >How to redirect 404 error page to homepage in WordPress using PHP
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.
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('url')); 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!