For a while now, I've been thinking about changing my URLs, instead of using the Movable Type id, they ought to use the date and the entry title. I followed some simple guidelines available and it was actually quite painless to do.
Then, I added a mod_rewrite rule to redirect any requests for the old-style URLs to a PHP-script which, by looking in the MT database, translates the URL to the new kind instead, and redirects the visitor there. Perfect.
My archives are stored like this:
/weblog/archives/2004/04/07/new_urls.php
But, by using tips from the above mentioned guidelines, I chop off the file extension from the URLs (using Brad Choate's regex plugin), and thereby display the permalinks like this:
/weblog/archives/2004/04/07/new_urls
When these URLs are requested, I use mod_rewrite to put the php-extension back on, which is invisible to the visitor. Hiding the file extension from the visitors is a good idea, since it makes the move from one server side technology to another transparent.
One problem remains though; MT uses the URL with the php-extension internally, which means that that's what it sends out with TrackBacks, and that's where it redirects after one leaves a comment. Thus, a visitor will see the php-extension-free URLs I prefer, unless he or she comes here via a TrackBack, or if he/she just left a comment, in which case MT will use the URLs with the php-extension. Not so good.
So, what I'd like is to redirect requests for new_urls.php to just new_urls, but that would create an infinite loop in mod_rewrite (I can't get it to work, at least, please tell me if your mad mod_rewrite skillz outshine mine). Either that, or hack MT internally, so it chops off the php-extension when sending TrackBacks and redirecting back from a comment. Hacking MT internally doesn't appeal to me very strongly, because I just know that it will end up being a problem for me when I upgrade to newer versions or something. I'd rather not go there (unless there's a plugin that can solve the problem).
So if you're a wizard with MT, or with mod_rewrite, please let me know.
Update: Changing where one gets redirected after posting a comment was easier than I thought. Can't seem to find anything on how to change what URL MT sends with TrackBacks though.
Comments
Rather than mess with either the regex plugin or mod_rewrite, I just have MT save entries as extensionless files and use an .htaccess file that ForceTypes anything within my archive directories to PHP. To ensure that each of those directories has the right .htaccess, I have that file saved as a monthly archive template so that new .htaccesses are created as new directories are.
Comment by David Demaree at 23:21, 07 Apr, 2004 #
David, I would be interested to know how did you do all that, step by step. Could you elaborate and provide examples, please? I would love to do what you did, since that would eliminate the "internal use" of extensions by MT... Please, shed some light. Thanks!
Comment by David Collantes at 00:11, 08 Apr, 2004 #
David: I'd have saved entries as extensionless files if I was serving straight HTML, I don't want Apache and PHP to interpret every file in the directory when they're being requested, especially since I'm planning on putting pictures and stuff which go with the entries in the archives, too (it seems like the obvious place to put them in).
Clever use of monthly archives though, although I don't see why you need it, since .htaccess rules are derived to subdirectories as well?
Comment by Tomas at 00:19, 08 Apr, 2004 #
David (Collantes): Check out Mark Pilgrim's guide and you'll see how he did just that. The only difference is that Mark uses vanilla HTML, so if you're using extensionless PHP files you'll have to add a couple of lines to the .htaccess file as well. Here's how:
<Files directoryname>
ForceType application/x-httpd-php
</Files>
That way, all files in that directory, and its subdirectories, are interptreted for PHP code. If you do this, you probably don't want to store any images in that directory, or any subdirectory, since they too will be interpreted for PHP code, that's the big drawback. Your mileage will vary, as always.
Comment by Tomas at 00:28, 08 Apr, 2004 #
Change your file extensions to .html rather than .php -- it's the most portable extension out there, which can be done with force type.
You may want to look into Apache's Multiviews directive, which means given a URL of /archives/2004/02/21/foo, it will look for 'foo*' (including foo/, foo.php, foo.html, etc).
The downside is that it will also look for foo/bah/, foo/bah/boo etc etc.
Comment by Justin French at 00:55, 08 Apr, 2004 #
Tomas, check you Már Örlygsson's tutorial on future proofing URLs, specifically Step 3. It appears to contain the Trackback hack that you're looking for (or at least something that could be adapted).
Comment by John B at 03:50, 08 Apr, 2004 #
Tomas: I've started keeping archive folders as children of my main blog directory (just to shorten my URLs by one level), and so ForceTyping everything inside /archives/ stopped being possible. It would have been great to be able to just DirectoryMatch or FilesMatch any folder that's a 4-digit number starting w/ '20,' but that didn't seem to work when I tried it. (Although I'm now going to check out your suggestion on using a Files directive -- it's an elegant solution, and it just might work on my server.)
The main advantage to doing htaccess files as MT templates is ease of use for people who (like me) aren't as well-versed in Apache, or stuck on virtual hosts that restrict what htaccesses can do. I know it's kind of like digging up a china cup with a steam shovel, but it gets the job done.
Justin: (And I say this as someone who isn't doing what Tomas is, but) If you're gonna use an extension at all, there's no reason not to use `.php` and save some work, if you're gonna have to ForceType anyway. Of course, one can also just put an AddType in their root directory to make .htmls run as PHP, which is that much simpler.
Technically valid, your suggestions are, but still more complex than necessary.
Comment by David Demaree at 03:52, 08 Apr, 2004 #
JohnB: That's one of the guides that I mentioned I read, but, as mentioned, I'm hesitant to actually hack the MT source code, I'd much rather use a plugin, if that's possible, or even leave it as it is.
I'm pretty content with the way it is now, as the only way to find the URLs with php-extensions is via TrackBack, and I think I can live with that.
I prefer no extensions to any extension, including html, it looks much cleaner, in my humble opinion.
Comment by Tomas at 12:24, 08 Apr, 2004 #
Tomas, Már Örlygsson's tutorial on future proofing URLs is very good (on the Trackback thing), and it works great. I have implemented it on my site. The only file that gets modified is Entry.pm (which I backed up first, of course). On any future updates I just keep that in mind --I document any changes I do to the base code-- and update accordingly. Now all my entries are cruft free. I will work on my categories next (which should be very easy).
Comment by David Collantes at 20:34, 08 Apr, 2004 #
Why not add a trailing slash (link?
Comment by MaThIbUs at 09:43, 09 Apr, 2004 #
Also, choose URI's wisely™ ;)
Comment by MaThIbUs at 09:45, 09 Apr, 2004 #
Mathibus: Actually, the URLs do work with a trailing slash as well, but that's just in case some browser or some person makes a mistake.
Comment by Tomas at 13:20, 09 Apr, 2004 #
The discussion has been closed on this entry. Thanks to everybody who participated.