A simple Apache recipe for migrating blog articles to a new host

By
Posted on
Tags:

In Everything old is new again, I wrote that I was moving articles from my old blog over to my new Typo-powered blog (here). Now that the process is underway, I need to make sure that people looking for my old articles can find them at their new home. To solve this problem, I am using a simple Apache httpd recipe to redirect requests for the old articles to the corresponding updated articles on my new blog. In case you need to do something similar some day, here is the recipe.

First, set up a mapping file

Create a two-column mapping file that you can use to map each article’s old location to its corresponding new location. If there are any parts of the locations that never change, you can factor them out to reduce clutter.

For example, the article “My New Radio VCR” has the following old and new locations (the constant parts are emphasized):

Old = http://community.moertel.com/ss/space/2004-02-20
New = http://blog.moertel.com/articles/2004/02/20/my-new-radio-vcr

Its entry in my mapping file looks like this:

# File: /path/to/conf/old-blog-to-new.txt
# Map articles from old blog to new blog.
#
# OLD LOCATION    NEW LOCATION
# .../ss/space/X  http://blog.moertel.com/Y

...               ...
2004-02-20        articles/2004/02/20/my-new-radio-vcr
...               ...

Second, configure Apache to use the mapping file

Edit the Apache configuration that controls the old locations. Add a set of mod_rewrite rules to match requests for the old locations and redirect them to the corresponding new locations, using the mapping file as a reference. For example, here is my configuration:

# in Apache's configuration for community.moertel.com

RewriteEngine on
RewriteMap blogmap txt:/path/to/conf/old-blog-to-new.txt
RewriteCond ${blogmap:$1|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/ss/space/(.+) http://blog.moertel.com/${blogmap:$1} [R=301,L]

The first line makes sure that mod_rewrite is active.

The second line tells Apache to load the mapping file. Apache will cache the mapping file’s contents for speed, but it is smart enough to reload the file when modified. That means you can add new entries to the mapping file at any time, and Apache will act on them immediately, no restart or reload required. Every time I moved an article over to the new blog, for example, I just edited the mapping file, and the new location “went live,” replacing the old.

The third line says that the recipe is conditional upon there being a matching entry in the mapping file. If no entry exists, the recipe will not apply, and the request will be handled as usual.

The final line defines the rewrite rule. In this example, it tries to match requests that start with “/ss/space/X”, where X is any suffix. (The prefix “http://community.moertel.com” is implied because this configuration is for the community.moertel.com site.) If the request matches, X is stored in the $1 variable. Then - and this is one of those things that makes mod_rewrite seem tricky - the condition defined in the previous line is tested using the current value of $1. If the condition is satisfied, the request is redirected to http://blog.moertel.com/Y, where Y is the corresponding location for X, according to the mapping file.

The [R=301,L] part of the rewrite rule is important. It specifies that redirects should be of the 301-Permanent variety. This advertises to the world that the new locations are intended to replace the old locations. Using permanent redirects also ensures that any Google juice that may have accumulated for my articles follows them to their new home.

Third, activate the new configuration

This part is easy: restart Apache to make sure it turns on the rewrite engine and activates the new configuration directives.

Finally, test it out

To see if everything is working properly, visit an article’s old location to see if you are redirected to the corresponding new location. For example:

If you click on this link, you should be redirected to blog.moertel.com.

And that’s the recipe.