Sat. Dec 21st, 2024

How to redirect 404 pages to homepage in WordPress?

404error

Solution #1:

Add some codes to your theme 404 template.
If there is a 404 page in your theme, for example, 404 Template (404.php), then

  1. Open 404 page
  2. Add the following code before the get header function, for example, “<?php get_header(); ?>“:

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header (“Location: /”);
?>

So now your 404 template page code looks like:

<?php
header(“HTTP/1.1 301 Moved Permanently”);
header (“Location: /”);
?>
<?php get_header(); ?>

Now, you can try one error to see does it work. I did mine and it works perfectly.

Note:

  • the 404 page in your theme usually at the top most list; it’s named something likes “404 Template (404.php)”.
  • if you’re unable to access to your site because of this message,
    “Parse error: syntax error, unexpected T_LNUMBER in /home/content/…/wp-content/themes/dynamic-news-lite/404.php on line 2″,
    then need to change the double quote marks by delete and retype them because sometimes and somehow the double quote marks are changed to a little bit off when we copy the codes; for example, I got double quote marks like “ and ” instead of stretch up like ” and “.
  • after updating the code in Theme editor, don’t ever get away the screen to test your site; just keep that screen there and then open your site in the new screen, so when there is something wrong because of whatever you just updated, you can change the codes back and don’t need to access your hosting account to fix the problem.

404error3

Solution #2:

Adding the code to .htaccess file.
The code to be added to the .htaccess file is

ErrorDocument 404 /index.php

I haven’t done this way yet because I tried the solution #1 and everything worked perfectly, so why do I have to try the solution #2 while the solution #1 working fine. That’s the reason I couldn’t tell you whether it works or not.

Solution #3:

WordPress 404 plugin.
I read somewhere on the internet there is another way to solve this problem by using WordPress plugin.  Once again, I haven’t try this solution yet, so I couldn’t tell you how it works.

(Visited 532 times, 1 visits today)

Leave a Reply