Use Apache's mod_rewrite for beautiful MovableType URLs
Posted in thoughts

If you are an URL fetishist like me, you certainly have been bugged by the fact that MovableType's installation URL kicks in when you need to use MovableType's search function.
Eventhough MovableType can host multiple weblogs each with their own domain natively, it requires a unique URL to its installation directory and the full path to its static directory. These parameters can't be customised on a weblog basis, and prevail for all weblogs driven by the same installation.
If you maintain multiple weblogs, needless to say that it's overkill in terms of support and maintenance to sport one MovableType installation per weblog. So what is the answer if you don't want the install URL to kick in when you run searches in your personalised weblog?
A symbolic link and an Apache's mod_rewrite rule will sort you out.
This weblog makes use of what follows. Although it doesn't offer a search function, it does have a tags page, with all tags being hyperlinked to related posts. This feature typically uses MovableType's search engine.
Moreover and just to complicate things, I use a generic third level domain that points to my MovableType installation: mt.cybmed.com. This brings up the login screen and all subsequent URLs for editing, posting or rebuilding use this domain. It's the domain used in the MovableType setup file (mt-config.cgi), and the search function uses it too.
First, symlink MovableType's installation directory to the weblog's cgi-bin directory. This way, all MovableType's URLs use my domain (davidroessli.com/cgi-bin). Unfortunately, this bans one from using system template elements such as <MTCGIPath> as its uses the configuration parameters. Use <MTCGIRelativeURL> instead, and check the default templates as most of them use <MTCGIPath>.
$ ln -s mt.cybmed.com/cgi-bin davidroessli.com/cgi-bin
Now everything behaves just like if MovableType was installed under davidroessli.com. But the URLs still suck. I can live with ugly admin URLs, but I despise ugly public URLs. So this is what I did:
- Rewrite your search URLs in your templates to get rid of the default ones;
- Rewrite your new search URLs with mod_rewrite to call the appropriate one.
My tags page uses the following template:
<div id="cloud">
<MTTags>
<h<$MTTagRank$>>
<a href="<$MTBlogURL$>tag/<$MTTagName$>"
title="This tag has been used <$MTTagCount$> time(s).
Click to search for related posts."><$MTTagName$></a>
</h<$MTTagRank$>>
</MTTags>
</div>
This above syntax can be used in your individual archive template in just the same way.
Add a line to your .htaccess file to catch the /tag/tag_name that looks like:
RewriteRule ^tag/(.*)$ /cgi-bin/mt-search.cgi?tag=$1&blog_id=nn&IncludeBlogs=nn
Where nn is the unique identifier of your weblog (check the URL after you've logged in to MovableType and selected your weblog).
Just the same can be done for searching:
RewriteRule ^search/(.*)$ /cgi-bin/mt-search.cgi?q=$1&blog_id=nn&IncludeBlogs=nn
So, with these two simple tips, I have a single MovableType installation to maintain, and all my weblogs use URLs belonging to their own domain that look pretty and are easy to remember.
Tip: I create a template for my .htaccess file. I find it much easier to use and maintain this way. Just make sure you don't rebuild it each time you rebuild your site. Another ressource to have handy when you're writing RewriteRules is Dave Child's mod_rewrite cheat sheet.
