Not so long ago, I reconfigured my Movable Type installation to so that it would write more intelligent archive URL’s. Before, it was writing /jimray/blog/archives/000128.html now it writes /jimray/blog/2003/03/my_entry_title.php.
This works great, since now all my past and future entries have a more human-readable URL. However, it also means that any older entries that may have been linked to or spidered by search engines aren’t consistent with the current blog. I wanted an easy mechanism to redirect all my old entries to the current version.
On Apache webservers, users can create something called an htaccess file that allows for configuration of the webserver on a per directory basis, without root access to the host machine. For instance, you can specify your own error page if the user gets a ‘404 - File Not Found’ error. You can also use the .htaccess file to redirect pages to another URL.
For instance, if you wanted the file foo.html in your web root to redirect to http://www.foo.com, your .htaccess file would look like this:
Redirect permanent foo.html http://www.foo.com
Easy as pie. However, it seemed like a monumental waste of time to go through 180 or so entries and manually map the old archive file name to the new one. I figured Movable Type could do a much better job.
I took a look at the old archive file name structure and compared it to the new archive name structure. In theory, at least, everything I needed was conviently available as a Movable Type template tag. Why not just create a template to generate the .htaccess file for me?
Before you can create the .htaccess template, though, you need to know a little bit about the filesystem of your webserver. Specifically, you need to know where in relation to the webroot directory your old archives are stored.
The webroot is the directory that holds the top level index file and all of the sub directories of the web server. So, if your Movable Type installation is set up to write your weblog at http://www.mysite.com/blog/ and all of your old archives are, by default, stored at http://www.mysite/blog/archives/, then you know with some degree of certainty that your setup is something like webroot/blog/archives. This is important and you should take a minute to figure out where your archives are in relation to the webroot directory.
With that in mind, I went to the Templates option of my Movably Type setup and created a new Archive Template, which I named htaccess, named the output file htaccess as well, and unchecked the “Rebuild template automatically…” option. This will write a file named htaccess in your current archives directory when you rebuild this new htaccess template.
The template itself is pretty simple — it builds the redirect instructions to point the old archive files to the new archive files. To do this, it loops through all of the individual entries in your Movable Type database and associates the old entry file name with the new entry file name.
<MTArchiveList archive_type="Individual"> Redirect permanent /blog/archives/<MTEntries><$MTEntryID pad="1"$>.html</MTEntries> <$MTBlogURL$><$MTArchiveDate format="%Y/%m/"$><$MTArchiveTitle dirify="1"$>.php </MTArchiveList>
The above provides a general template for a .htaccess file, you may need to adjust according to your specific setup. Take a very good look at the above template and determine how your own weblog works in relation, please don’t just copy and paste verbatim.
Save the template and then rebuild only the htaccess template file. This will create a file in the archives directory of your weblog called htaccess that should look something like this:
Redirect permanent /blog/archives/000067.html http://www.mysite/blog/2003/03/my_title.php
At this point, the redirect isn’t happening automatically. You might want to manually check a few of the entries to make sure that everything is pointing in the right direction (i.e. archives/000067.html really is the same as 2003/03/my_title.php). Now, just move the htaccess file generated by Movable Type to webroot/.htaccess (note the dot [.] before htaccess). If you’re logged in to a Unix machine and the htaccess file is located in the webroot/blog/ directory, this should work for you:
mv htaccess ../.htaccess
To test things out, manually type in the address for one of your old archives (http://www.mysite.com/blog/archives/000067.html) and it should automatically redirect to the new URL.
When I was playing around with the .htaccess file in the webroot of my site, I was continually frustrated by the utter failure of the .htaccess redirect to perform as advertised. My old archive entries kept getting returned, indstead of properly redirecting. I figured this had something to do with the fact that my website (http://www.ibiblio.org/jimray/) is actually a subdirectory of ibiblio.org.
So, the webroot is actually one directory up from my personal root directory. Since .htaccess works relative to the main webserver root, you need to take this into consideration if your weblog is configured similarly. For instance, my .htaccess file looks something like this:
Redirect permanent /jimray/blog/archives/000001.html http://www.ibiblio.org/jimray/blog/2002/06/all_the_fuss.php
Your .htaccess file will still go at the top level of your personal website directory, not the webroot, though, since you probably won’t have write permission on that directory anyway.
Now that your pages are properly redirecting, it’s safe to delete (or, even better, backup then delete) your old archive pages. This will keep search engine spiders from indexing your old archive files in the future and will help to clear out your old files from search engine indexes. And you’ll continue to help make the web a more beautiful place.