Chapter 6: Publishing
Enabling Dynamic Publishing
Problem
You want to enable the dynamic publishing features in Movable Type.
Solution
In most cases, enabling dynamic publishing is as simple as configuring the blog's publishing settings to publish dynamically and performing a full rebuild.
Discussion
A SQL database (i.e. MySQL, SQLite or PostgreSQL) is required in order to utilize the Dynamic Publishing feature. Dynamic publishing in Movable Type also requires the web server's support for PHP as well as either custom error documents or mod_rewrite. Most server environments meet these requirements with no extra configuration. If you are administering your own server and want the details on setting it up, please see the section below on Configuring your webserver.
1. Enable dynamic publishing
On the Publishing tab of the blog's settings page, select whether to publish all of your archive templates dynamically or set your templates' publishing modes individually. See Specifying the Publishing Model.
2. Perform a full rebuild
Performing a full rebuild after enabling dynamic publishing will ready the filesystem for dynamic publishing by moving aside any static files previously published by templates which are not set to render content dynamically.
If, for some reason the steps above did not enable dynamic publishing for the pages produced by the templates you specified for dynamic publishing, see the section below on "Troubleshooting dynamic publishing".
How the dynamic publishing engine works
The dynamic publishing engine works by leveraging the web server's ability to respond to the absence of a requested page. In the case of the Apache webserver, this is done either via mod_rewrite or a custom error document directive located in the .htaccess file. In the case of Microsoft Internet Information Services (IIS), it is done using a custom error document for the blog's directory.
In all cases, when web server cannot find the requested file, it is directed (via the .htaccess file or IIS custom error document setting) to serve up content from a the script, mtview.php. This script, known in the default templates as the "Dynamic Site Bootstrapper", handles the request and looks in Movable Type's internal file recordkeeping database for the requested URL.
If a record for the requested page is found, the content of the page is compiled on-the-fly and returned to the client. In addition, it is cached in the templates_c directory to eliminate this step for subsequent requests.
If a record for the page is not found, Movable Type compiles the contents of the Dynamic Pages Error Template (found in the blog's System templates) and returns it to the client to inform them that the file could not be found.
In all cases, if a file exists on the webserver corresponding to the requested URL, the file will be served to the client and the dynamic publishing engine will never be called. This is why a full rebuild is required, during which all static files previously published by the templates you have chosen for dynamic publishing a moved aside by the appending of the string .static to the filename.
Configuring your webserver
If you are an administrator looking to configure your webserver to support Movable Type's dynamic publishing engine, you should make sure that your server is configured as specified in the section below based on your webserver type.
PHP support must be enabled regardless of the web server in use.
- Apache
You should configure your Apache server to use mod_rewrite if it isn't already enabled. You can use the httpd -l command to list available Apache modules. If the module is dynamically loaded, check your httpd.conf file for a line that begins with LoadModule rewrite_module.
Apache should also be configured to utilize .htaccess files. This also is specified in the httpd.conf file for your Apache installation. An example looks like this:
<Directory />
AllowOverride All
</Directory>
Depending on your server configuration, this may go in the global context or within a particular VirtualHost section.
- Microsoft IIS
To support dynamic publishing on an Internet Information Service server, you must configure it to use the mtview.php script to handle errors, allowing Movable Type to intelligently decide when and how to create documents dynamically.
In the Internet Information Services management console, navigate to your Web Sites, and then to the folder where your blog is published. Right-click on this folder and choose "Properties" from the menu to edit its settings.
The last tab of the Properties dialog box will let you edit Custom Errors for this blog. On this tab, you'll see a list of Error Messages for HTTP Errors. Scroll down to the listing for "404", click on it to select it, and then click "Edit Properties..."
The Error Mapping Properties box that pops up will let you select a Message Type for 404 errors. Choose "URL" as your type, and then in the URL box, type in the URL where your published blog is accessible, followed by mtview.php. For example, if you publish your blog to /updates under the C:\Inetpub\wwwroot\ folder, you'll type in /updates/mtview.php.
Click OK to enable these changes to your error mapping settings, and then OK again to commit your changes to the Properties for your publishing folder. You should now be able to enable dynamic publishing for any or all of your templates, and you can test your results by visiting your published blog by clicking "View Site" from within Movable Type.
Troubleshooting dynamic publishing
.htaccess for Apache
When the dynamic publishing option on the blog's Publishing settings page is enabled, Movable Type attempts to create an .htaccess file in the blog's site root directory. If however, one already existed in that location and contained custom content not produced by Movable Type, the application will not alter or replace the existing one. If this describes your situation, see the section below entitled "Dynamic publishing .htaccess file".
mtview.php
On all supported webservers, the dynamic publishing engine requires the presence of the Dynamic Site Bootstrapper, mtview.php in the blog's site root directory. Also, this template must be published statically by Movable Type in order to render other files dynamically.
If, for some reason, you no longer have this template, create a new index template, set the Output File to mtview.php and copy in the following code (always accessible from here) into the template body, save and rebuild:
<?php
include('<$MTCGIServerPath$>/php/mt.php');
$mt = new MT(<$MTBlogID$>, '<$MTConfigFile$>');
$mt->view();
?>
...and make sure that it is not configured to be rendered dynamically.
templates_c directory
On all supported webservers, the dynamic publishing engine requires the presence of the templates_c directory in the blog's site root directory. If the templates_c directory doesn't exist, you can create this yourself. It must have read, write and execute permissions for the webserver (this is usually 777 or -rwxrwxrwx).
Dynamic publishing .htaccess file
If you have a custom .htaccess file in the blog's root directory and hence one cannot be published by Movable Type, the following code can be appended to it to support dynamic publishing for the blog.
Note however that if your blog's site root is located in a subdirectory the domain site (e.g. http://www.example.com/blog), you will need to adjust the all instances of the path to mtview.php in the script below to match the actual location of this file. For instance, if the URL to mtview.php is http://www.example.com/blog/mtview.php, you would use /blog/mtview.php.
# Disable fancy indexes, so mtview.php gets a chance...
Options -Indexes +SymLinksIfOwnerMatch
<IfModule mod_rewrite.c>
# Add mtview.php to the list of DirectoryIndex options,
# listing it last, so it is invoked only if the common
# choices aren't present...
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.htm default.htm default.html default.asp /mtview.php
</IfModule>
RewriteEngine on
# don't serve mtview.php if the request is for a real
# directory (allows the DirectoryIndex lookup to
# function)
RewriteCond %{REQUEST_FILENAME} !-d
# don't serve mtview.php if the request is for a real
# file (allows the actual file to be served)
RewriteCond %{REQUEST_FILENAME} !-f
# anything else is handed to mtview.php for resolution
RewriteRule ^(.*)$ /mtview.php [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
# If mod_rewrite is unavailable, we forward any missing
# page or unresolved directory index requests to mtview.
# If mtview.php can resolve the request, it returns a 200
# result code which prevents any 4xx error code from
# going to the server's access logs; however, an error
# will be reported in the error log file. If this is your
# only choice, and you want to suppress these messages,
# adding a "LogLevel crit" directive within your
# VirtualHost or root configuration for Apache will turn
# them off.
ErrorDocument 404 /mtview.php
ErrorDocument 403 /mtview.php
</IfModule>



