• nocabt

    (@nocabt)


    We migrated a site recently for a client, and I’m having one issue with redirecting their old posts. Basically, their old site, due to poor design, could have multiple (up to 10!) URLs for each post/article, in varying formats, with no clear logic, like:
    domain.com/something1/article-slug/something2/something3/
    domain.com/article-slug/something1/something4/
    etc….
    There are 5,000 + posts in the DB, plus all the crazy incorrect URLs they have which are indexed by Google.

    Therefore, htaccess rules have not been sufficient as far as I’ve been able to figure out. I was able to redirect a huge chunk of them using some rules, but there are a lot still throwing 404s (manual rules for each post not possible due to shear qty of URLs). My ideas to fix is this:

    1. Any request for a URL that throws a 404 should explode the URL and loop through the array to see if the article-slug exists in the WP post_name field (which was migrated over)

    2. If it does, redirect 301 to:

    domain.com/post_name/

    3. If not, 404.

    My only problem is how to integrate this well into WP. Currently I added the script to the top of the 404.php template file, but by that point there has already been a 404 header sent. I’m sure I can intercept this BEFORE it’s sent so that the proper response is given to our friend Google but I’m not sure how to do that with WP.

    Or, maybe there is another solution?

    Much thanks,

    Thomas

Viewing 2 replies - 1 through 2 (of 2 total)
  • Chris

    (@zenation)

    You could add something like this to you’re theme’s function.php or create a plugin for that:

    add_action( 'template_redirect', 'checkOldURLs' );
    
    function checkOldURLs() {
    
      if( !is_404() ) {
        return;
      }
    
      /* do the URL checking... */
      if( ... ) {
        wp_redirect( $newLocation, $statusLike301 );
        exit;
      }
    
    }
    Thread Starter nocabt

    (@nocabt)

    template_redirect is what I needed, that did it. Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect script and 404 errors’ is closed to new replies.