Update thanks to David Weingart for pointing out the typo in the mod_rewrite rule below.
Rasmus Lerdof’s Apache 404 error handler is a clever hack for regenerating content on the fly, however, I don’t like the idea of using an error handler to deliver a non-error URL.
Pete of RasterWeb beat me to pointing out there’s a more elegant way to handle this using mod_rewrite.
So, here’s how I’d do Rasmus’ cache funk:
In httpd.conf or .htaccess:
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^.+\.html$ \
handlers/render.php?%{QUERY_STRING}
We don’t bother passing the original URI, because Apache sends it to PHP and makes it available to your script as $_SERVER['REQUEST_URI']
Then handlers/render.php looks like:
<?php
include_once('renderer.class.php');
// Renderer is your PHP class
// that digests the URI and
// GET parameters and returns
// a complete HTML page.
$renderer = new
renderer ($_SERVER['REQUEST_URI'],$_GET);
$fp = fopen ($_SERVER['DOCUMENT_ROOT'] .
"/" . $_SERVER['REQUEST_URI']);
$data = $renderer->getRenderedPage ();
fputs ($fp,$data);
close ($fp);
print $data;
?>
Note that you have to either run PHP in safe mode (so that it executes the script as your user) or have your server’s DOCUMENT_PATH owned by the user running httpd.
Instead of writing the result to the file system, you might want to squirt the finished page into MySQL so you can decide in your script when you want to fetch the page from the table, and when to expire it.
Better yet, PHP could get persistent, ‘Application Level’ objects in which I stash the rendered page, and avoid the file system altogether.
