Index of /dev/null

getting rid of MT's permalink file extensions

While designing scalable and portable web projects, it's always a good idea to design hyperlinks independently of the physical file path from where the respective request should be served then.

For instance, I usually have permalinks looking like this one:

http://beta-blog.net/2010/02/getting-rid-of-mts-permalink-file-extensions
Thus I can decide later on whether I prefer to serve pages as html, shtml, php, or whatsoever - without losing the permanentness of my permalink Now, while I have choosen static publishing with HTML extension, MT creates a file such as
/var/www/beta-blog.net/2010/02/getting-rid-of-mts-permalink-file-extensions.html
so I can tell Apache how it should map my permalink to the physical file path using mod_rewrite:
# DocumentRoot /var/www/beta-blog.net
RewriteEngine on
RewriteBase /
RewriteRule ^([^\.]+[^\/])$ $1.html [L]

That is, any request path not containing a dot and not ending with a trailing slash will be rewritten to the according HTML file. (Yes, that way I won't be able to serve permalinks containing dots, but I can live with that ;)

Now that's nice, but unfortunately, within MT you are not able to configure the looks of permalinks inedpendently from the file path. Nobody wants to remove the extension from the actual file, since the same directory might contain some .js, .jpg and other files mapped to their own content types as well. (And I have no idea how to set up an AddHandler directive exclusively respecting files without extension.) Therefore, the solution is to let MT add the configured extension to the actual file, but remove the extension from the permalink.

A blessing in disguise, one has to hack MT's source code itself, but it's a quite simple change to the archive_url method within MT's Entry module. Thus, applying the following patch to lib/MT/Entry.pm will remove .php/.html extensions from archive links:

--- MTOS-5.01-en.orig/lib/MT/Entry.pm   2010-02-14 01:17:24.000000000 +0100
+++ MTOS-5.01-en/lib/MT/Entry.pm        2010-02-14 01:58:58.000000000 +0100
@@ -541,7 +541,11 @@
     my $blog = $entry->blog() || return;
     my $url = $blog->archive_url || "";
     $url .= '/' unless $url =~ m!/$!;
-    $url . $entry->archive_file(@_);
+    #$url . $entry->archive_file(@_);
+    ## --> HACK: remove .php/.html extensions <-- ##
+    my $f = $entry->archive_file(@_);
+    $f =~ s/\.(php|html)$//;
+    $url . $f;
 }

 sub permalink {

Well, thats really a dirty hack and things will get more complicated with dynamic publishing. So, hopefully one day MT's developers will implement a cleaner solution and make it configurable through the webinterface. *hack*

no comments

post a comment

Leave a comment
You may use HTML tags for style