A few weeks ago, Kristine posted about Redirects in MT, and this got me thinking. I had already looked at an archive mapping solution which was recently updated, but it required that I have access to the server config files (not just a .htaccess file) because a RewriteMap can only be defined in the server config files.
So tonight when I decided, after a great deal of pondering (about 10 minutes), to change my archiving format to something that didn't produce 1000+ numerically named directories I needed a way to map the archive numbers to their new named location. Here is the solution I came up with, based on some modifications of the pre-existing code.
First download mt-archivefinder.cgi and rename it to mt-archivefinder.cgi (or any other name that suits your purposes). Put it in the MT directory.
You will need to change a couple of the lines at the top of the file to match your server's configuration. Make sure to chmod the file 755.
Next you will need to do some mod_rewrite mojo with a .htaccess file. Create a .htaccess file or edit the existing file in the root of your archives directory. To that file add:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/archive/[0-9]{6}.html
RewriteRule ^(.*).html$ /mt/mt-archivefinder.cgi?$1
You will need to change it to reflect your path structure. If all goes well, you will be automagically redirected from any numbered file like /archives/000001.html to whatever new archiving scheme you enter into the individual archive template in the Weblog Config panel in MT.
For example, I was using
<$MTEntryID pad="1">/index.php
and now I'm using
<$MTArchiveDate format="%Y"$>/<$MTArchiveDate format="%m"$>/<$MTEntryTitle dirify="1"$>/index.php
I used the following mod_rewrite rule to automatically forward all links to the old archive style to the new archive style.
RewriteCond %{REQUEST_URI} ^/history/[0-9]{6}/
RewriteRule ^(.*)/$ /mt/mt-archivefinder.cgi?$1
That's all it takes!
Many thanks to Alex for the inspiration and the code.