You should redirect on the server, not on the client
So, in you server side code, perform browser detection by using USER_AGENT -
have a look here[
^] here which has examples in every language.
If you know the request is coming from a mobile based browser, then redirect to the appropriate sub-domain.