<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Tom Moertel's Weblog: A simple Apache recipe for migrating blog articles to a new host</title>
    <link>http://blog.moertel.com/articles/2006/02/06/a-simple-apache-recipe-for-migrating-blog-articles-to-a-new-host</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>A simple Apache recipe for migrating blog articles to a new host</title>
      <description>&lt;p&gt;In &lt;a href="http://blog.moertel.com/articles/2006/01/21/everything-old-is-new-again-moving-content-over-from-my-old-blog"&gt;Everything old is new
again&lt;/a&gt;,
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.&lt;/p&gt;


	&lt;h3&gt;First, set up a mapping file&lt;/h3&gt;


	&lt;p&gt;Create a two-column mapping file that you can use to map each
article&amp;#8217;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.&lt;/p&gt;


	&lt;p&gt;For example, the article &amp;#8220;My New Radio &lt;span class="caps"&gt;VCR&lt;/span&gt;&amp;#8221; has the following old and
new locations (the constant parts are emphasized):&lt;/p&gt;


	&lt;table style="background: #eee; margin-bottom: 2ex;"&gt;
		&lt;tr&gt;
			&lt;td&gt; Old &lt;/td&gt;
			&lt;td&gt; = &lt;strong&gt;http://community.moertel.com/ss/space&lt;/strong&gt;/2004-02-20  &lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt; New &lt;/td&gt;
			&lt;td&gt; = &lt;strong&gt;http://blog.moertel.com/&lt;/strong&gt;articles/2004/02/20/my-new-radio-vcr &lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;Its entry in my mapping file looks like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# 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
...               ...
&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Second, configure Apache to use the mapping file&lt;/h3&gt;


	&lt;p&gt;Edit the Apache configuration that controls the old locations.
Add a set of 
&lt;a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html"&gt;mod_rewrite&lt;/a&gt; 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:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# 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]
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The first line makes sure that mod_rewrite is active.&lt;/p&gt;


	&lt;p&gt;The second line tells Apache to load the mapping file.  Apache will
cache the mapping file&amp;#8217;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 &amp;#8220;went live,&amp;#8221; replacing the old.&lt;/p&gt;


	&lt;p&gt;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.&lt;/p&gt;


&lt;p&gt;The final line defines the rewrite rule.  In this example, it tries
to match requests that start with &amp;#8221;/ss/space/&lt;em&gt;X_&amp;#8221;, where _X&lt;/em&gt; is any
suffix. (The prefix &amp;#8220;http://community.moertel.com&amp;#8221; is implied because
this configuration is for the community.moertel.com site.) If the
request matches, &lt;em&gt;X&lt;/em&gt; is stored in the &lt;code&gt;$1&lt;/code&gt; variable.
Then &amp;#8211; and this is one of those things that makes mod_rewrite seem
tricky &amp;#8211; the condition defined in the &lt;em&gt;previous&lt;/em&gt; line is tested using the current
value of &lt;code&gt;$1&lt;/code&gt;.  If the condition is satisfied, the request
is redirected to http://blog.moertel.com/&lt;em&gt;Y_, where _Y&lt;/em&gt; is the
corresponding location for &lt;em&gt;X&lt;/em&gt;, according to the mapping file.&lt;/p&gt;

	&lt;p&gt;The &lt;code&gt;[R=301,L]&lt;/code&gt; 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
&lt;em&gt;replace&lt;/em&gt; the old locations.  Using permanent redirects also ensures
that any &lt;a href="http://en.wikipedia.org/wiki/Google_juice"&gt;Google juice&lt;/a&gt; that
may have accumulated for my articles follows them to their new
home.&lt;/p&gt;


	&lt;h3&gt; Third, activate the new configuration&lt;/h3&gt;


	&lt;p&gt;This part is easy:  restart Apache to make sure it turns on the
rewrite engine and activates the new configuration directives.&lt;/p&gt;


	&lt;h3&gt; Finally, test it out&lt;/h3&gt;


	&lt;p&gt;To see if everything is working properly, visit an article&amp;#8217;s
old location to see if you are redirected to the corresponding
new location.  For example:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://community.moertel.com/ss/space/2004-02-20"&gt;http://community.moertel.com/ss/space/2004-02-20&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;If you click on this link, you should be redirected to blog.moertel.com.&lt;/p&gt;


	&lt;p&gt;And that&amp;#8217;s the recipe.&lt;/p&gt;</description>
      <pubDate>Mon, 06 Feb 2006 17:52:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e15c26dc481101280414328cf179e6b0</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2006/02/06/a-simple-apache-recipe-for-migrating-blog-articles-to-a-new-host</link>
      <category>site news</category>
      <category>web development</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/51</trackback:ping>
    </item>
  </channel>
</rss>
