Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.
Next, you want to go to your .htaccess file and add something like this:
RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]
The first RewriteCond
contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond
checks if the host isn't already m.example.com
. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule
itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1
gets replaced with the first capture group in the regex in the first argument of RewriteRule
. The [R]
flag is not necessary in this case (links with http://
will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L]
flag will stop processing more rules if this rule matches.
See the documentation for more information.