example.com is your main website
m.example.com which means "m" stands for sub-domain in your website. "m" also points to a particular folder in your web directory.
For example in Cpanel
user1/public_html/index.html <- this index file will be loaded when you type example.com.
but for "m" or whatever sub-domain name is, its like this
user1/public_html/m/index.html <- by default to access this you will do example.com/m
so you need to configure the sub-domain in your admin panel so that instead of typing
example.com/m you will type
m.example.com that points on the same directory.
as for detecting whether the user should go to
m.example.com you may use