Movable Type: Where Am I?

The vServer that hosts our own and our clients’ web sites is based (loosely) upon the Movable Type blogging engine. In the versions we’re currently deploying (2.661/3.11), there appears to be no embedded way to get at the current page URL for any page on the site – MTEntryPermaLink will give you the effective URL for an individual entry, but that’s a special case. So what to do? Enter php, stage left, with smug grin: Now I’m a complete newbie at php, so there may well be much easier way of doing it, but what the hell – this one works, at least in my production environment, which is, FYI, Mac OS X/X Server 10.3.5/Apache 2.0.47/php 4.3.2/MT2.661.


Step 1: Make sure your site is enabled for php – the site’s httpd.conf file should contain the following lines as global directives (or however your host sets up your virtual server):
LoadModule php4_module modules/libphp4.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Step 2: Make sure that your individual site will allow php parsing from .html files as well as .php. To do so, do one of:
amend the Global “AddType application/x-httpd-php .php” directive to “AddType application/x-httpd-php .php .html” (without the quotes);
Insert the directive “AddType application/x-httpd-php .html” into your local virtual host definition;
create or amend a .htaccess file in your site’s home directory to contain the same directive as the previous option.
Of these, I tend to prefer the second – I don’t like managing lots of individual .htaccess files and turning on php parsing for all hosted sites on your server, especially if you don’t control all the coding, can cause undesireable security holes to appear and can also cause entire sites to fail unceremoniously, due to…
Step 3: If your xhtml code contains an encoding definition of the form <?xml version=”1.0″ encoding=”iso-8859-1″?>, then you’ll need to modify it otherwise if will cause the page load to fail with a php parsing error. There are a couple of ways around this – so far, I’m simply replacing the statement with <?php echo “<?xml version=\”1.0\” encoding=\”iso-8859-1\”?>”;?>, which simply causes php to echo back the original statement to the browser.
Step 4: Now for the easy bit, finding out your local page URL: Wherever you need to reference, pass or display the current page URL, simply include the statement <? echo ‘http://’,$_SERVER[‘HTTP_HOST’], $_SERVER[‘PHP_SELF’] ?>. Bingo. For a specific example, see … where I’m using this to construct a command string to pass to the TranscendRT machine translation engine at freetranslation.com.
You may of course be constrained in what you can do if you use a hosting service – I do have the luxury of managing my own web servers. And I’ve received a number of requests to turn the whole thing into a Movable Type Plugin, which seems like a good idea, so watch this space –  just don’t hold your breath.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.