TwitterFacebookGoogle

Getting WordPress URL migrations to work when all else fails!

Recently, I modified the permalinks for my blog posts from: http://nizarnoorani.com/index.php/archives/%postid% to just be: http://nizarnoorani.com/%postname%

This new permalink structure will now apply to any new posts that I create. Which of course begs the question:

Will the links to all my existing blog posts break??

And the answer sadly and obviously is: Yes, they will!

Can we fix this unfortunate issue? Sure, we can! And there are quite a few WordPress plugins that do exactly this. A search for “WordPress URL migration plugins” will serve up links to many such plugins. One that I particularly liked and recommend is Advanced Permalinks.

If installing/activating/configuring one of these plugins fixes your problem – great! Things for me, however, weren’t quite so easy.

For some reason, which I still haven’t been able to figure out, none of the migration plugins worked for me! Maybe its because my blog was originally hosted under Windows and then I later moved it to Linux. Or maybe I wasn’t configuring the plugins correctly. Or maybe something else. I don’t know.

Anyways, IF you are in the same boat then here is one hacky solution to get your existing permalinks working again. Once again – Only do this IF you can’t get the WP migration plugins to work!

Okay, here goes:

  1. Log in to your WordPress Dashboard: http://yourwebsite.com/wp-login
  2. Set up your new permalink structure by navigating to Settings -> Permalinks. After you set up your new permalink structure, you’ll notice that your existing permalinks will now be re-directed to a 404 Not Found page. Browse to one of your existing permalinks to verify that this is the the case.
  3. Okay, time to put in our little hack to get the existing permalinks working again. Open up the WordPress 404 template page inside the WordPress editor by navigating to Appearance -> Editor and clicking on the 404 Template link on the right-hand side bar.
  4. Insert the following code snippet just below the get_header() line:
    <script type='text/javascript'>
     
    <?php 
     
         // extract the post_id from the URL. In my case, my existing permalink was of the 
         // structure http://nizarnoorani.com/archives/%postid%.
         // modify the extraction code below for our particular structure as needed.
         $postId = (int)trim($_SERVER['REQUEST_URI'], '/archives/');
     
         $post = get_post($postId);
     
          if($post != null) {
             // again, modify the URL as needed to match your new permalink structure.
             echo "window.location = 'http://nizarnoorani.com/".$post->post_name."';"; 
         }  
    ?>
     
    </script>

    Note: Be sure to modify the code snippet to match your specific new and old permalink structures!

  5. Save your changes. Your existing permalinks should start working again!

Basically, what we’ve done here is added some logic to extract the post_id from the old link, retrieve the post with that post_id from the database, construct the new URL and then re-direct the user to the new URL.

Cheers!

Twitter Email Linkedin Digg Stumbleupon Subscribe

Leave a comment

Your email address will not be published.


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

CyberChimps