Set up a 404 error handler in .htaccess:
RewriteEngine on
RewriteRule /.*\.[^h][^t][^m][^l]$ /$1.html
ErrorDocument 404 /index.php
DirectoryIndex index.php

index.php:
<?php
if (!empty($_SERVER['REDIRECT_URL'])) {
    
// This is the requested page that caused the error
    
$current_page substr($_SERVER['REDIRECT_URL'], strlen(WEBBASE));
}

...

if (!
FORCE_DYNAMIC) {
    
$contents ob_get_contents();
    
ob_clean();
    
$f fopen($lang "/" $current_page ".html"'w');
    
fwrite($f$contents);
    
fclose($f);
    echo 
$contents;
}
?>