Turn 404's to 301's

Discussion in 'Blogging' started by drunkpunk, Aug 6, 2010.

  1. drunkpunk

    drunkpunk Newbie

    Sep 27, 2008
    Likes Received:
    I have an auto-blog that is basically a news aggregate. I had around 30,000 posts and about 5,000 tags which caused my plug-ins and server to begin having issues. I decided to just keep the posts from the last 45 days as well as tags that were used more than 5 times and delete the rest. I was able to delete what i needed and saw immediate improvement on the back end of my site. However, I then noticed traffic started slipping. This wasn't because of missing links however because I know from my tracking that the only links people are entering are news stories from within the last two weeks at the latest. I then read that when you delete web pages that are indexed G starts collecting 404 errors from spiders or the sitemap and does not like that.

    So, to the point, is there a way to automatically make all my 404 errors become 301 redirects to my homepage or a page that says the item they are looking for is expired ? There is a plug-in I've read about called "objection redirection" that sounds like it would do this automatically so 404 errors are never detected, however I can not find an active download link and their website is never up.

    Any suggestions?
  2. tiero

    tiero Newbie

    Aug 6, 2010
    Likes Received:
    I'd be happy to enlighten you. It's really just setting up url-rewrites in a specific manner. If you can't google-find it, then just let me know.
  3. appleman

    appleman Regular Member

    Oct 30, 2009
    Likes Received:
    delete things? get a bigger server ... htaccess is what your looking for
    Last edited: Aug 6, 2010
  4. drunkpunk

    drunkpunk Newbie

    Sep 27, 2008
    Likes Received:
    appleman - I have the small plan on hostgator and not enough money yet to upgrade. As soon as I can I will so I don't have to mess with this.

    tiero - I didn't know about url-rewrites. I googled it and will do some reading up. I will let you know if I still need help.
  5. Keedev

    Keedev Regular Member

    Apr 2, 2008
    Likes Received:
    You might try this.

  6. drunkpunk

    drunkpunk Newbie

    Sep 27, 2008
    Likes Received:
    Thanks Keedy, the only problem with that is i have to maually turn all the 404's it collects into 301's... I read through their instructions and help, do you know a way to make it automatically detect all 404's and make them redirect to a certain page?
  7. demoniox

    demoniox Registered Member

    Mar 5, 2007
    Likes Received:
    save this as 404.php in your theme directory

    $ASKAPACHE_S_C = array(
    '400' => array(
      'Bad Request', 
      'Your browser sent a request that this server could not understand.'),
    '401' => array(
      'Authorization Required', 
      'This server could not verify that you are authorized to '.
      'access the document requested. Either you supplied the '.
      'wrong credentials (e.g., bad password), or your browser '.
      'doesn\'t understand how to supply the credentials required.'),
    '402' => array(
      'Payment Required', 
    '403' => array(
      'You don\'t have permission to access THEREQUESTURI on this '.
    '404' => array(
      'Not Found', 
      'We couldn\'t find <acronym title="THEREQUESTURI">that uri'.
      '</acronym> on our server, though it\'s most certainly not '.
      'your fault.'),
    '405' => array(
      'Method Not Allowed', 
      'The requested method THEREQMETH is not allowed for the URL '.
    '406' => array(
      'Not Acceptable', 
      'An appropriate representation of the requested resource '.
      'THEREQUESTURI could not be found on this server.'),
    '407' => array(
      'Proxy Authentication Required', 
      'This server could not verify that you are authorized to '.
      'access the document requested. Either you supplied the wrong '.
      'credentials (e.g., bad password), or your browser doesn\'t '.
      'understand how to supply the credentials required.'),
    '408' => array(
      'Request Time-out', 
      'Server timeout waiting for the HTTP request from the client.'),
    '409' => array(
    '410' => array(
      'The requested resourceTHEREQUESTURIis no longer available on '.
      'this server and there is no forwarding address. Please remove '.
      'all references to this resource.'),
    '411' => array(
      'Length Required', 
      'A request of the requested method GET requires a valid '.
    '412' => array(
      'Precondition Failed', 
      'The precondition on the request for the URL THEREQUESTURI '.
      'evaluated to false.'),
    '413' => array(
      'Request Entity Too Large', 
      'The requested resource THEREQUESTURI does not allow request '.
      'data with GET requests, or the amount of data provided in the '.
      'request exceeds the capacity limit.'),
    '414' => array(
      'Request-URI Too Large', 
      'The requested URL\'s length exceeds the capacity limit for '.
      'this server.'),
    '415' => array(
      'Unsupported Media Type', 
      'The supplied request data is not in a format acceptable for '.
      'processing by this resource.'),
    '416' => array(
      'Requested Range Not Satisfiable', 
    '417' => array(
      'Expectation Failed', 
      'The expectation given in the Expect request-header field could '.
      'not be met by this server. The client sent <code>Expect:</code>'),
    '422' => array(
      'Unprocessable Entity', 
      'The server understands the media type of the request entity, but '.
      'was unable to process the contained instructions.'),
    '423' => array(
      'The requested resource is currently locked. The lock must be released '.
      'or proper identification given before the method can be applied.'),
    '424' => array(
      'Failed Dependency', 
      'The method could not be performed on the resource because the requested '.
      'action depended on another action and that other action failed.'),
    '425' => array(
      'No code', 
    '426' => array(
      'Upgrade Required', 
      'The requested resource can only be retrieved using SSL. The server is '.
      'willing to upgrade the current connection to SSL, but your client '.
      'doesn\'t support it. Either upgrade your client, or try requesting '.
      'the page using https://'),
    '500' => array(
      'Internal Server Error', 
    '501' => array(
      'Method Not Implemented', 
      'GET to THEREQUESTURI not supported.'),
    '502' => array(
      'Bad Gateway', 
      'The proxy server received an invalid response from an upstream server.'),
    '503' => array(
      'Service Temporarily Unavailable', 
      'The server is temporarily unable to service your request due to '.
      'maintenance downtime or capacity problems. Please try again later.'),
    '504' => array(
      'Gateway Time-out', 
      'The proxy server did not receive a timely response from the '.
      'upstream server.'),
    '505' => array(
      'HTTP Version Not Supported', 
    '506' => array(
      'Variant Also Negotiates', 
      'A variant for the requested resource <code>THEREQUESTURI</code> '.
      'is itself a negotiable resource. This indicates a configuration error.'),
    '507' => array(
      'Insufficient Storage', 
      'The method could not be performed on the resource because the '.
      'server is unable to store the representation needed to successfully '.
      'complete the request. There is insufficient free space left in your '.
      'storage allocation.'),
    '510' => array(
      'Not Extended', 
      'A mandatory extension policy in the request is not accepted by the '.
      'server for this resource.')
    // prints out the html for the error, taking the status code as input
    function aa_print_html ($AA_C){
        if($AA_C == '400'||$AA_C == '403'||$AA_C == '405'||$AA_C[0] == '5'){
            @header("Connection: close",1);
            if($AA_C=='405')@header('Allow: GET,HEAD,POST,OPTIONS,TRACE');
            echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>";
            echo "<title>$AA_C $AA_REASON_PHRASE</title>";
            echo "<h1>$AA_REASON_PHRASE</h1>\n<p>$AA_MESSAGE<br>\n</p>\n</body></html>";
            return true;
        } else return false;
    // Tries to determine the error status code encountered by the server
    if(!isset($_REQUEST['error']))  $AA_STATUS_CODE = '404';
    else  $AA_STATUS_CODE = $_REQUEST['error'];
    if(isset($_SERVER['REDIRECT_STATUS']) && $_SERVER['REDIRECT_STATUS']!='200') 
    $AA_THE_REQUEST = htmlentities(strip_tags($_SERVER['REQUEST_URI']));
    array('The server encountered an internal error or misconfiguration '.
    'and was unable to complete your request.',$AA_THE_REQUEST,$AA_REQUEST_METHOD));
    if($AA_STATUS_CODE == '404') :
    	header( "HTTP/1.1 301 Moved Permanently" ); 
    	header( "Location: ".get_bloginfo( "wpurl") ); 
    // begin the output buffer to send headers and resonse
    @header("HTTP/1.1 $AA_STATUS_CODE $AA_REASON_PHRASE",1);
    @header("Status: $AA_STATUS_CODE $AA_REASON_PHRASE",1);
        <?php get_header();?>
        <div id="content">
        <div class="post">
        <h1><?php _e("$AA_STATUS_CODE $AA_REASON_PHRASE"); ?></h1>
        <?php if(function_exists('aa_google_404')) aa_google_404(); ?>
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>
    <?php } 
    exit; exit();
  8. leolion

    leolion Junior Member

    Jul 3, 2010
    Likes Received:
    rather than deleting your posts, use some good wp cache plugin and get a VPS. burst.net has cheap linux vps.

    but still if you decide to delete posts, you can use 404 SEO plugin or add following code to your theme's 404.php page.

    header("HTTP/1.1 301 Moved Permanently");
    header("Location: ".get_bloginfo('url'));
    • Thanks Thanks x 1
  9. drunkpunk

    drunkpunk Newbie

    Sep 27, 2008
    Likes Received:
    leolion - went with your fix and just tossed that into the beginning of the 404.php It is sending 404's to the homepage which is perfect. I had a bunch of information in my 404.php because it displayed a custom page from the theme I was using. Do i need any information besides your code anymore since it will redirect everything to my homepage and no one will ever see the 404 page now?

    demoniox - Thanks for the code. any advantage of yours over just putting in the one part of leo's?