Subscribe to our RSS Feed
feed image
Redirect Old Links To New Wordpress Blog Using Apache
I recently revamped my website into an entirely Wordpress-driven system (for those of you that didn't know the site before), and I ran into a slight problem.

After updating my Wordpress installation with all the posts from my previous site, I decided that I wanted to have the "pretty permalinks" for each post. That basically means that instead of the rather ugly and cryptic URLs normally associated with PHP, they would appear according to their actual name (which is also more search engine-friendly).

For instance, if the post "Hello World" was post number 12, the url might be "http://www.learnaboutlinux.net/index.php?p=12". That's ugly. Instead, I want the URLs to be somewhat readable, by organizing them according to date and name. So, if that post was created on 11-20-08 (today), then the URL would be "http://www.learnaboutlinux.net/2008/11/20/hello-world/". Much better, huh? It's more heirarchical, and you can get an idea of what the post is simply by looking at the URL. So how is this done?

This is done (at least, in Apache) by using Apache's mod_rewrite module.  You first have to activate the module in Apache (in Ubuntu, a simple "sudo a2enmod rewrite" will do the trick).  I won't really go any further on that, since it's not the point of this post.  Once you've enabled the module, you simply edit the .htaccess file within the DocumentRoot of your site (that's the main directory where your webpage is located).  Your .htaccess file may contain something like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

I won't go into it too much, but it will basically get your URLs looking nice (as long as you've also enabled these nicer URLs in your Wordpress settings as well).  Now, here's the big issue I ran into:

I had so many posts from my old site that won't have a valid URL any longer!  So many people have linked back to my posts, and I'll be killing all of those links by changing everything around!

Let's take my "Dell and the Vista Monster" post (don't worry if you have no idea what that is, it's just an example).  The link for that post on my old site was "http://www.learnaboutlinux.net/dell_vista.htm".  The link for that post on my new site is now "http://www.learnaboutlinux.net/2007/03/28/dell-and-the-vista-monster/".  So, if someone were to visit the old link, it wouldn't work, right?  Well, yes... but we have to do some tweaking first!

Back to that .htaccess file.  Apache's mod_rewrite module is incredibly able (and useful).  We'll simply add a directive to tell Apache to watch out for that old link, and upon receiving it, rewrite it into the new link:

RewriteRule ^dell_vista\.htm$ /2007/03/28/dell-and-the-vista-monster/

But wait, you've already got some rewrite rules in there!  So once you've rewritten the old link to the new link, the rewrite rules you've already got in there will mess up your URL!  The solution is actually to add this line BEFORE your global rewrite rules, and to rewrite the link to the "ugly" URL format (because those ugly URLs are going to be rewritten by the later rules anyway).  Once you rewrite the URL to the ugly format, then it gets rewritten again by the rules you already had, to look like the nice URLs you were hoping for!  This is what your new .htaccess file might look like:

RewriteEngine On
RewriteRule ^dell_vista\.htm$ index.php?p=23
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

See what I did?  I renamed the original link to its link in the ugly format, by specifying the "index.php" file, plus the additional data (Wordpress uses "?p=N", where "N" is the post id).  Once you have your old posts in your new Wordpress blog, you can find the post id for a post by hovering your mouse over it, in the "Manage Posts" section of your admin interface (you can look at the linked address in the status bar).

And that's it!  You can continue this method by adding a line for each of your old links (I have to warn you though, it can get really tedious if you have a lot of posts... my .htaccess file is pretty loaded).  This is why you shouldn't move links once they're on the Internet, lol.
Last Updated on Sunday, 08 February 2009 04:17
 

Add your comment

Your name:
Your email:
Comment: