I was wondering if anyone can give me an opinion on the best way to redirect a site to another url when a mobile phone opens the page.

Since I'm using wordpress, my first thought was to create a page with a domain http://www.example.com/mobile and assign a page template to it, and write a small function in the function.php to redirect using $_SERVER['HTTP_USER_AGENT']. I would rather have the domain as something like m.example.com by using sub domains but I don't think that would work with permalinks, if it can be done with permalinks, please tell me

Any tips/links for tutorials/suggestions anyone can give me will be greatly appreciated.

Thanks
-Charles