Use Apache's mod_rewrite for beautiful MovableType URLs
Posted in thoughts
Tags :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.
Comments and responses
08 Jun 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.
18 Nov 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.
19 Nov 2008
@Will: I believe they should work just fine :)
21 Jan 2009
Thanks for the tutorial. Is there any easy-to-understand tutorial on symlink that might make this a little more accessible to those of us who are petrified of the command line?
I’ve done clean tag-search URLs through .htaccess before, but with my current installation structure (domain1.com and domain2.com using an MT installation in user.host.com), the old rewrite rules don’t seem to work. This looks to be a solution, but I’m not familiar with symlink.
21 Jan 2009
Never mind. I think I figured it out.
Thanks again for the tutorial.
22 Jan 2009
@Culture Snob: try this one on MacOSXHints:
http://www.macosxhints.com/article.php?story=2001110610290643
this weblog entry:
http://www.cocoatech.com/weblog/archives/2007/04/30/aliases_symboli
or the manual page:
http://developer.apple.com/documentation/Darwin/Reference/Manpages/man1/ln.1.html
Cheers
02 Feb 2009
Thanks for the tutorial David, it helped me a lot.
However, how do you deal with the mt-static folder. Now that it is symlinked in the cgi-bin folder, It seems like I can’t point to it from my new domain (I’m using MT’s StyleCatcher which creates a @import in my CSS to a style inside mt-static.).
Using your technique, should one have a separate mt-static folder per domain/blog? Or could one simply create a symlink of the mt-static folder somewhere out of the cgi-bin folder? Not sure if I’m very clear…
Thanks again! // e
02 Feb 2009
@enrique: thanks!!
You are right to point this out. I left it out of the above tutorial beacuse none of its URLs appear in the address bar.
I believe you could symlink a`mt-static` folder into your blog’s folder, and it should work just fine.
The configuration takes relative paths too (you don’t have to specify a full URL):
StaticWebPath /mt-static/
So creating a symlink like,
ln -s /somewhere/on/your/disk/mt-static/ /your/blog/mt-static/
should do the trick.
WRT StyleCatcher, I’m not sure about it’s default behaviour. I’ll give it a look.