Where are your domains registered? If you transfer domains to a registrar like gandi.net, they'll handle your DNS for you. So that you can have domain2.com pointed directly to domain1.com/domain2
This works well for me.
Other than that I'm not really sure. You might doing a search for DNS hosting. Or you might try reading the URL from the header, and then redirecting based on that.
I'd go for having it done on the dns level, though.
Good luck.