Skip to:
Content
Pages
Categories
Search
Top
Bottom

Enhance your 404 page

  • The default 404 page doesn’t have much of value to offer a visitor that stumbles your website via an organic search result. This is why I have enhanced mine 404 page a bit and I would like to share that with you guys.

    Add 301 redirect for 404 pages

    Pages that have been removed to a different URL, will show a 404 page. Add a 301 redirect to your 404 page that will redirect the user to its new address.

    Suppose you had an address domain/old-url this got moved to domain/new-address, add the follow code to your 404.php (located in your bb-templates folder). Add this to the top of the page.

    <?php

    $redirect_301 = array(

    '/old-url/' => '/new-url',

    '/another-old-url' => '/another-new-url'

    );

    $request = $_SERVER;

    if (array_key_exists($_SERVER, $redirect_301)) {

    header(“HTTP/1.1 301 Moved Permanently”);

    header(‘Location: http://www.yourdomain.com&#8217; . $redirect_301[$request] );

    die();

    }

    ?>

    Also you can change the default text:

    <p><?php _e('I'm sorry, but there is nothing at this URL.'); ?></p>

    Into something more nice, like:

    <p><?php _e('The page you are looking for can not be found or it has been removed. You can always just browse the website or even use the search form below to try another search.'); ?></p>

    <?php search_form(); ?>

    What do you guys think? :D

Viewing 2 replies - 1 through 2 (of 2 total)
  • do u have any solution for pretty permalinks using custom 404 running on windows iis??

    i would like to know please

    <?php
    $redirect_301 = array(
    '/ddd/' => '/forum/newlocation',
    );

    list( $request, $query ) = explode( '?', $_SERVER['REQUEST_URI'], 2);
    $request = rtrim( $request, '/' );

    $forum = dirname( $_SERVER['PHP_SELF'] );

    foreach( $redirect_301 as $from => $to ) {
    $from = $forum . '/' . trim( $from, '/' );
    if( $from != $request )
    continue;
    if( !empty( $query ) )
    $to .= '?' . $query;
    wp_redirect( bb_get_uri ( $to ), 301 );
    exit();
    }

    ?>

    That might support IIS, the native WP function doesn’t use the Location: header so I guess that’s an issue?

    http://support.microsoft.com/kb/q176113/

    @chandersbs: It’s nice :D I tidied it up to support trailing slashes no matter what and query strings amongst other things. I think. That or it’ll break :D

    I dropped the need to have the forum directory included in the $redirect_301 array as well, that’s automatically added now. Just change bb_get_uri ( $to ) to $to if you need to point the redirect outside the forum directory or use ..

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar