Use Apache's mod_rewrite for beautiful MovableType URLs

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.
Ø permalink: http://www.davidroessli.com/logs/2007/04/use_apaches_mod_rewrite_for_be/
TrackBack URL for this entry:
http://www.davidroessli.com/cgi-bin/mt-tb.cgi/49.
Previous: Airport Disk hangs logout if not unmounted
Next: Damp and powerless


Reponses to “Use Apache's mod_rewrite for beautiful MovableType URLs”
#1 by gid
06:12 on 8 June 2007
This is close to what I have been looking for. The problem is that the keywords in your urls are separated by underscores, which almost defeat the purpose of rewriting the urls. I think most people rewrite their urls for search engines and the little extra boost they give.
#2 by Will
05:40 on 18 November 2008
Great tutorial. I've got the same issue, a separate domain for a single MT install driving multiple blogs.
I want to your technique for one of the blogs I've got running, but don't want to break the others.
Say I've got Movable Type installed at:
http://mt.example.com/
publishing different blogs to different domains (all on the same server):
http://domain1.com/
http://domain2.com/
http://domain3.com/
If I create a symlink between...
mt.example.com/cgi-bin and domain1.com/cgi-bin
...what happens with the other domains that are referencing mt.example.com/cgi-bin? Will they still work as usual with the ugly urls or will they break?
Thanks.
#3 by David Roessli
08:17 on 19 November 2008
@Will: I believe they should work just fine :)