X

How to Redirect 404 Pages in WordPress

There may be times when visitors come to your site and land on 404 pages which mean nothing to them and the visitor simply bounces away. This is a wasted visit for you as well as a poor experience for the visitor. One way to avoid this is creating custom 404 pages for your site. However, this activity is time consuming and may not be suitable for all sites. In this case, one useful tip is to redirect the user to the site home page or to another page with content.

Redirect 404 to Home Page

Here’s how to redirect 404 page to home page in WordPress:

  1. Open your 404.php file in your theme’s folder. If it doesn’t exist, then create a blank php file.
  2. Paste the following code there at the beginning of the file:
    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".get_bloginfo('url'));
    exit();
    ?>
  3. Save the file.
  4. Your’re done. Now on, whenever a user hits a 404 page, he or she will be redirected to the homepage.

Redirect 404 to Custom URL

Here’s how to redirect 404 page to a custom page in WordPress:

  1. Open your 404.php file in your theme’s folder. If it doesn’t exist, then create a blank php file.
  2. Paste the following code in there preferable at the beginning of the file:
    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://mysite.com/mypage");
    exit();
    ?>

    Replace http://mysite.com/mypage with the url of the page you want to redirect to.

  3. Save the file.
  4. Your’re done. Now on, whenever a user hits a 404 page, he or she will be redirected to the the url specified above.

Note: Ideally, you should try and fire the analytics events before redirecting so that you can track the 404 pages on your site and fix the broken links. However, this is more complex to do.

Did this help you? If yes, please drop us a note in the comments below. Feel free to share this article with your friends using the social buttons below. Thanks!

Comment Policy: Comments adding value to the article are encouraged. Relevant links will be allowed in such comments.
If you think that you have a link that adds value to this article please contact us at techie[at]techzog[dot]com for evaluation of inclusion into the article.
Comments left solely for spamming links will be deleted. Thank you for understanding.
Varun: Passionate blogger and main author at Techzog.com. Feel free to reach me at techie[at]techzog[dot]com
Related Post