I'm a little confused: You want your 404 page to display a nonexistent page?
$REDIRECT_URL contains the url of the requested-but-missing item.
If you want to build a pull-based caching system, grab basename($REDIRECT_URL), pull out any info that's required to do the db query (using one of the regex functions), build the page, store it in the requested location, then passthru() the results.