- b2evolution Support Forums
- b2evolution Support
- General Support
- HOWTO: Redirect old WordPress URLs to B2evolution
#1 Sep 26, 2006 01:09
Note: This is not a tutorial on migrating a WordPress blog to B2evolution. This is about using URL rewriting (via mod_rewrite) to map WordPress fancy URLs to a B2evolution post or search results page. Read the back story for more details.
The Back Story
As I've mentioned in several other posts already, I recently lost my old WordPress blog installation due to a botched server upgrade. Having no good database backup available (clearly in violation of my own motto "Save early, save often"), I was at least able to use Google's site search to save cached copies of my content, which I've then been slowly back-posting to my new B2evolution blog.
Unfortunately, I used trackbacks a lot in my old blog, so there are many outdated links on other blogs that point to my now invalid WordPress installation. Not to mention all of the old links that still show up in some search engine results. I couldn't just install B2evo over the old WP folder since the folder and URL structures were so different as to have required redirects any ways. At first, my solution to this was a generic redirect to my new blog for any request to the old URL. This was a quick hack, and got me up and running with B2evolution, but it meant that anyone looking for an old article was SOL.
The quickest way to redirect on a post-by-post basis would have been to just forward the old post slug to the new blog, but this posed two problems - 1.) B2evolution post slugs are shorter than in WP, so a direct match isn't always possible; and 2.) WP encoded slugs differently than B2evo does. Besides, since I was back-loading over time, many old slugs would simply return no results so it would have been a fairly useless fix for a while.
After toying around with several configurations, I finally settled on a hybrid solution that was part mod_rewrite and part B2evolution hack. This allowed me to do a simple catch-all redirect but still do some complex pattern matching in order to serve up as close to the correct thing as possible.
This is still a work in progress, and any suggestions for improvement are welcome. The various parts are listed below with descriptions following each section.
Part 1 - the .htaccess file
My old WordPress folder had it's own .htaccess file which I made a back up copy of (just in case) and replaced the contents with this:
Part 2 - the receiving script
This requires a hack to the /inc/_blog_main.inc.php file in B2evolution. I usually try to avoid hacking source files directly, preferring to use the extended plugin architecture available. However, in this case it was just quicker to hack. If I end up porting it to a plugin at some other point I'll update this post (feel free to contribute your own too!).
We want to insert our code after the last call to $Request->param() which should be on or around line 102. Here's what I inserted: (I included a few lines of the original for reference before and after the custom code)
Explanation of added code
Part 3 - disambiguation
This part is optional, but it helps make your searches more accurate. At the end of each entry that I'm back posting I add an HTML comment that includes the text from the old WordPress slug, minus any non-alphanumeric characters. It's not guaranteed that my old post title will also appear in the original post content, so this seeds the article with text that is sure to be found using the search as formed in part 2. For instance, if my old post had the slug "it-slices-and-dices-and-julians-fries", I would add this to the end of the post content:
The "backpost :: " part just makes it easier to find in the database later on if need be. Remember, since the WordPress slug is likely to be different from the new B2evo slug this gives us a better chance of finding the correct article. I could copy/paste, but since the B2evo slugs are shorter I'm still not guaranteed an exact match.
That's it (plus I'm late for dinner). If you find any bugs (which are likely just typos since it's working on my blog) or have any suggestions please let me know!