Redirecting subdomain traffic

I have several subdomain sites that I am moving to a single domain and want to know how to ensure old urls don’t break.

i.e.

Old sites:

subdomain1.domain.com
subdomain2.domain.com

On the new site will be located on the new combined site:

domain.com/subdonain1
domain.com/subdonain2

How can I ensure old URLs still resolve, for example

subdomain1.domain.com/testfile1.html
subdomain2.domain.com/test/testfile2.html

Should resolve to:

domain.com/subdonain1/testfile1.html
domain.com/subdonain2/test/testfile2.html

How can I set this up? Thanks