One of the advantages of Movable Type over Wordpress comes from publishing static HTML files (fetching data from the database for each page request doesn't scale well). By creating static HTML files, round trips to the database are eliminated and your blog is more likely to handle sudden bursts of high traffic. How many times have you gone to a popular Wordpress blog and seen database errors like "too many database connections"?
Additionally, Apache generates ETag and Last-Modified headers for you on static HTML files, and automatically sends 304 Not Modified headers as well. If you don't know why this is important, read the definitive introduction to caching -- we'll assume you have a solid foundation in the basics and won't review that material here. Suffice it to say, caching and proper headers are important to save server CPU and bandwidth.
However, static HTML files are, well, static. Converting Movable Type blogs to PHP is simple, but if you've already published as HTML you'll have all new URL's after converting to PHP -- a situation we want to avoid. But the big problem for scripting languages like PHP, ASP or CGI is Apache won't automatically handle ETag's or 304 Not Modified headers. Every time a page is requested, the full page is sent -- even if it's not needed. That wastes bandwidth.
We want to avoid that, and it's simple -- we'll tackle it in two parts.
Configure Apache to parse HTML files with PHP
First, in your .htaccess file, add the line
AddType application/x-httpd-php .php .html
This configures Apache to treat both php AND html files as PHP. If you don't have any PHP code in your HTML files, the PHP interpreter simply passes the HTML along. However, by doing this you'll notice the ETag and Last-Modified headers are gone. And thus your ability to properly cache the page as well. That's bad, but not hard to fix.
NOTE: NEVER EVER test on your production server. Always have a test environment to play with. Testing on your production server is begging for problems. You should read and understand the code before using it.
To get proper headers back when using PHP, use the following script and place it at the top of your PHP files with a line like the following: <?php require($_SERVER['DOCUMENT_ROOT'] . "/php-caching.php"); ?> (assuming you've placed the script in your web servers root directory).
That's it. You'll now have appropriate headers and caching is enabled. Read the script and note you can control the Expires header (default expiration is 1 minute). Also note the script uses the file date of the page as the Last-Modified time. Normally this is what you want, but if it's not you'll have to change the code.
Movable Type and PHP
One more thing for Movable Type needs to be addressed. Some of Movable Type uses Perl CGI, and we don't want to inject PHP on those pages. Dynamic CGI isn't cachable anyway, so it's not a big issue, but PHP code won't work on Perl pages. Depending on how modified your Movable Type installation is (and what version), it could be as simple as placing the following in your header template:
<MTUnless name="system_template"> < ?php include($_SERVER['DOCUMENT_ROOT'] . "/php-caching.php"); ?> </MTUnless>
(Remove the space between the < and ?php)
Of course, you'll need to experiment with your specific setup to be sure if works for you. But you'll notice the Perl CGI's are marked as system templates. Of course, YMMV.
As always, TEST TEST TEST! Every installation is different and requires testing to be sure it all works in your unique situation -- MT is highly customizable so it's not a one size fits all proposition. The script is only about 50 lines so should be easy to follow. Read the code before using it and be sure you understand what it's doing.
References
- PHP XHTML and MIME type Another article on this site with introduction to caching, and includes XHTML mime type detection to properly serve XHTML as application/xhtml+xml.
- PHP XHTML and MIME type Part II Builds on the previous article with improvements.
- Caching Introduction The best caching information on the Internet. Period. Read it -- it's really that good.
Like this article? Stay up to date with new articles and content. It’s free, and we won’t sell your information.
SIGN UP TODAY by entering your primary email address below to guarantee you won’t miss anything.
