2) in Apache, using mod_rewrite, if the request is for HTML (or the app, whatever that looks like) rewrite the request into a PHP handler. If it's not (ie it's a graphic request or something) then let it flow through.
The important part about this, is that you're going to hijack all requests to <the old application> into your PHP handler... which is going to throw the request back into Apache. The only reason you'd need to use a different port is if you can't put another domain on the box, or perhaps you don't want to use 127.0.0.1 ... or something. You could make "internal.mydomain.com" be "127.0.0.1" and then put the handler for the application on THAT virtual host, rather than your main domain. Since all calls are coming into your PHP handler rather than the application, you'll need to call the application yourself to let it do it's thing before you can manipulate it.
Although this is slightly different (and I REALLY don't want to know if this is in fact what you're up to), the functional metaphor for this structure would be a "Man In The Middle" attack... you're hijacking requests, then proxying them to the actual handler, getting your hands on the answer before sending it back to the user.
Recapping now: calls to the original domain get routed into a PHP handler, as illustrated below. Calls for graphics and such are immediately handled and don't get rewritten. The PHP routine rethrows the request into the server, so when results come back they are now in your hands in the PHP script, where you can have your way with it before you send it back to the surfer.
RewriteEngine on
# First - if the call is in the /graphics dir, or it is suffixed by .gif or .jpg then
# do no further rewriting...
RewriteCond %{REQUEST_URI} /graphics [OR]
RewriteCond %{REQUEST_URI .gif [OR]
RewriteCond %{REQUEST_URI} .jpg
RewriteRule ^(.*)$ - [L]
# Rewrite all else into the app...
RewriteRule ^(.*)$ /www/sites/mainhandler.php?$1
Now in PHP you use any number of ways to throw a request right back up against the server ie., cURL, fopen, file_get_contents, there's a bunch of ways. There's even a webrequest class in the php repository here. You'll need another virtual host:
<VirtualHost 127.0.0.1:80>
ServerName 127.0.0.1
DocumentRoot /aDir/aDir
</VirtualHost>
This host is the one that is now answering requests for your application.
In PHP, when you get the "answer" back from the application, it'll be all text and then you can have your way with it... finally simply doing a "echo $theModifiedStringBuffer;" so that the final output that you want to go to the user, does.
Is that any clearer? I know for some this is difficult to wrap their brains around.
Good luck,
/p