301 redirecting broken links after moving Joomla! to subdomain and giving parent domain to Drupal7?
I would personally follow the steps described below:

1) Use this technique to detect the 404s and issue the redirect

2) Since you don't want to issue the redirect right away, but first check whether the URL exists in the Joomla! subdomain, you can use a function like this to determine whether the URL exists.

3) In order to determine what the URL of the incoming request was (so that you can use it to match it against the Joomla! subdomain), you can use this: $_SERVER['REDIRECT_URL']