Monday, February 18, 2008

Static file caching with Apache mod_rewrite

Apache's mod_rewrite can check whether a file exists. This can be used to implement a file caching mechanism where the output from a script is cached and served as a static file. This will typically be faster than a caching mechanism than requires running an interpreter (eg. PHP) on every request but requires the whole page to be cached. For example, put the following in your Apache config: RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ../load_page.php
On a cache miss load_page.php generates the page contents and writes it to the cache/disk and sends it to the browser.

The cached files must be deleted by a separate process when the underlying content changes.

No comments:

Post a Comment