- Moving all my old posts to a “archive” category.
- Adding code to single.php in my theme to extract all the posts in that category to a search string on the new site with the title of the post, hoping wordpress’s search function would catch it (which it did fairly well)
- However, mobile themes and other problems didn’t make this elegant.
- And, this isn’t forward-compatible with anything else that happens on the old website.
So, I kept thinking about how to do this… perhaps with a script running very early in loading WordPress, or perhaps with .htaccess??
Then, I realized that all my “old” posts happen to be in a “folder” of either the year 2008-2012 thanks to wordpress’ permalinks. Looking into it, I found that redirecting the “permalink” URL on my old site to my new site was enough for the new site to determine which post it was and rewite the URL appropriately. So, with a little nosing around, the elegant solution appears to be:
- Update .htaccess with the following line:
RedirectMatch 301 /2012(.*) https://robertmerrill.wordpress.com/2012$1
- This basically reads the incoming URL and if it’s in the base folder “/2012” then it takes the URL and redirects it to the new domain with a trailing “/2012” on it. The “$1” appends the rest of the URL from the original link into the new URL.
- The “301” after “RedirectMatch” specifies that this is the permanent redirect location for this link ( and that search engines should ignore the old link and use the new link instead)
- Then, I added additional lines for posts from the years 2008-2011.
- Save, upload the file and automagically, all my old posts redirect! No theme munging required, and this works very fast and across all future updates to ConnectedWell.
What’s especially awesome is that WordPress can figure out where the new post is on the new domain even through the old post’s permalink structure!
- So…. the URL from the old site: http://connectedwell.com/2012/grief-and-healing-after-sandy-hook.htm
- Gets redirected to: https://robertmerrill.wordpress.com/2012/grief-and-healing-after-sandy-hook.htm (note JUST the domain name is changing here)
- But WordPress smartly changes it to fit the new domain’s permalink structure: https://robertmerrill.wordpress.com/2012/12/22/grief-and-healing-after-sandy-hook/ (this is WordPress default… “/year/month/day/slug/”)