Forum Moderators: phranque
Welcome to WebmasterWorld!
This is fairly easy to do using mod_rewrite. Perhaps the references provided in our forum charter [webmasterworld.com] will help get you started.
The 'pieces' that you'll need are mod_rewrite's RewriteCond directive, used to test and extract the currently-requested domain and subdomain (test the %{HTTP_HOST} variable), and RewriteRule, to test the requested URL-path, build the new URL using back-references to the hostname and requested 'page', and generate the 301 redirect.
There are also many examples of using mod_rewrite to test and change subdomains posted here in this forum, some very recent. Examining the code in those posts and reading the discussion may prove worthwhile.
Jim