Simply redirect a 404 to a custom page

Hi,

-how can we simply add some code to redirect all 404’s to a custom page?

Thanks!

  • Rupok
    • Support Ninja

    Hi M.

    Hope you had a wonderful day.

    You can simply redirect all 404’s to a custom page by editing the 404 page template inside your active theme directory. In your theme directory, you will see a 404.php file. Add the following code at the top of that file and save:

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

    Replace “http://www.google.com” with your desired custom page URL.

    Please let us know if you have any confusion. We will be glad to help.

    Have a nice day. Cheers!

    Rupok

  • D.
    • The Crimson Coder

    Hi, we’ve added this however is causes a syntax error:

    Parse error: syntax error, unexpected ';' in /nas/content/staging/emprendedor/wp-content/themes/Child/404.php on line 4

    <?php get_header(); ?>
    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://emprendedor.staging.wpengine.com/buscando-algo/";);
    exit();
    ?>

    <div id="content" class="full-width">
    <div id="post-404page">
    <div class="post-content">
    <?php
    // Render the page titles
    $subtitle = __( 'Oops, This Page Could Not Be Found!', 'Avada' );
    echo Avada()->template->title_template( $subtitle );
    ?>
    <div class="fusion-clearfix"></div>
    <div class="error-page">
    <div class="fusion-columns fusion-columns-3">
    <div class="fusion-column col-lg-4 col-md-4 col-sm-4">
    <div class="error-message">404</div>
    </div>
    <div class="fusion-column col-lg-4 col-md-4 col-sm-4 useful-links">
    <h3><?php _e( 'Here are some useful links:', 'Avada' ); ?></h3>
    <?php
    if ( Avada()->settings->get( 'checklist_circle' ) ) {
    $circle_class = 'circle-yes';
    } else {
    $circle_class = 'circle-no';
    }
    wp_nav_menu( array( 'theme_location' => '404_pages', 'depth' => 1, 'container' => false, 'menu_id' => 'checklist-1', 'menu_class' => 'error-menu list-icon list-icon-arrow ' . $circle_class, 'echo' => 1 ) );
    ?>
    </div>
    <div class="fusion-column col-lg-4 col-md-4 col-sm-4">
    <h3><?php _e( 'Search Our Website', 'Avada' ); ?></h3>
    <p><?php _e( 'Can't find what you need? Take a moment and do a search below!', 'Avada' ); ?></p>
    <div class="search-page-search-form">
    <?php echo get_search_form( false ); ?>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    <?php get_footer();

    // Omit closing PHP tag to avoid "Headers already sent" issues.

    Probably we’re entering this in the wrong section right?

  • Ash
    • WordPress Hacker

    Hello @m.

    I hope you are doing good.

    The correct code should be:

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://emprendedor.staging.wpengine.com/buscando-algo/");
    exit();
    ?>

    Remove all other codes from this page and use the ONLY the above code :slight_smile:

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers

    Ash

  • Kasia Swiderska
    • Support nomad

    Hello M.

    Please try this code instead in your theme fuctions.php:

    function wpmudev_js_redirect_404() {

    if ( is_404() ) {
    ?>
    <script>
    window.location.replace(" https://emprendedor.staging.wpengine.com/buscando-algo/");
    </script>
    <?php
    }
    }

    add_action('wp_footer','wpmudev_js_redirect_404');

    kind regards,

    Kasia

  • Rupok
    • Support Ninja

    Hi M.

    Hope you had a wonderful day.

    How can we now track our 404’s?

    I’ve found an article describing how you can track your 404 pages in Google Analytics, here: http://www.labnol.org/internet/track-404-error-pages/13509/

    Here’s another way to track your 404 pages: http://www.wpbeginner.com/plugins/how-to-track-404-pages-and-redirect-them-in-wordpress/

    Can you please try that and let us know if it works for you?

    Have a nice day. Cheers!

    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.