Forum Moderators: phranque

Message Too Old, No Replies

On-the-fly Content-Regeneration w/ mod_rewrite

How does this work w/ other rules?

         

dwilson

6:34 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



I just read about this feature at [engelschall.com...]
Problem Description:

Here comes a really esoteric feature: Dynamically generated but statically served pages, i.e. pages should be delivered as pur static pages (read from the filesystem and just passed through), but they have to be generated dynamically by the webserver if missing. This way you can have CGI-generated pages which are statically unless one (or a cronjob) removes the static contents. Then the contents gets refreshed.

Problem Solution:

This is done via the following ruleset:

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond ^page\.html$ page.cgi [T=application/x-httpd-cgi,L]

Here a request to page.html leads to a internal run of a corresponding page.cgi if page.html is still missing or has filesize null. The trick here is that page.cgi is a usual CGI script which (additionally to its STDOUT) writes its output to the file page.html. Once it was run, the server sends out the data of page.html. When the webmaster wants to force a refresh the contents, he just removes page.html (usually done by a cronjob).

Now, suppose that I don't have a page.cgi for each page, but want /foo/bar.html to go to page.php?cat=foo&page=bar but want to use the content regeneration.

Which rule comes first? Or does it become one very complicated rule?

Thanks!

jdMorgan

7:05 pm on Apr 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hah! Cool typo you found there... There is no Rule, they are both Conds!

You have a couple of choices. First you can make your single script responsible for regenerating all missing files which are requested. Or, you could put a "wrapper" around your existing script -- one wrapper for each file which might need to be be regenerated, and then have individual rules (or one smart rule) to do the redirects. Basically, you have to pass along the name of the requested missing page to whatever entity is responsible for re-creating it if it's missing, whether that is a bunch of individual scripts, or a single combined script.

In the case of single combined script, you'd just put the script name into the substitution of the RewriteRule -- which is what that second line should have been, not a RewriteCond -- and append the name of the file that needs to be regenerated as a query string.

Jim