So you want to redirect that shinny new mobile site? No problem! I’ll show you how to detect mobile browsers in a few simple steps. When I first started designing mobile sites, I searched for the easiest way to redirect my sites and found a lot of fluff. I played around with different scripts and then found gold. The script below will detect a mobile browser and is super easy to implement.
Enough talk, let’s get into the good stuff:
Detect Mobile Browser Script Instructions
- Visit Detect Mobile Browser and download the script. Just right click on the Script link and click Save As. Save it to your desktop. (Rename the file to redirection_mobile.js if it has any other name)
- FTP into your site and make a folder in the root called js. My folder is located at https://mobilewebrockstar.com/js. Upload the file downloaded in Step 1.
- Now copy and paste this piece of code in between the <head> (A bunch of code will be here) </head> of your homepage:
<script type="text/javascript" src="js/redirection_mobile.js"></script>
<script type="text/javascript">
SA.redirection_mobile ({param:"isDefault", mobile_prefix : "m", cookie_hours : "1" });
</script>
And that’s it! Super easy to do and you’ll be soon rocking your new mobile site.
Setup a Mobile Subdomain
After your phone detects a mobile browser you will need to redirect them somewhere. I send my sites/clients sites to a m.theirsite.com domain (subdomain). You might have already setup a mobile subdomain but if not, then here are the instructions. If you’re using CPanel (my host is HostGator and they use CPanel) then follow these steps. If not the process might be similar. If you’re not sure then email your web host that you want to create a subdomain and they should be able to sent you instructions.
- Log into your CPanel.
- Under Domains click Subomains.
- Type in m for the subdomain. IMPORTANT! I originally screwed this step up. Your root is going to be yoursite/m. Might even be (public_html/yoursite/m).
- Now just copy your mobile files to that m folder on your host. If you don’t have a m folder then create one in the root of your site.
You’re mobile subdomain is now setup and ready to rock!
Detect Mobile Browser VIDEO
It’s super easy to setup this detect mobile browser script. Just follow the instructions in the video.
The mobile web is exploding and will soon be the main way people view the internet. If you’re creating a mobile site for yourself or you clients then you need to redirect them to their mobile site. This is the quickest and easiest way I found to detect mobile browsers. I will update this page if I come across other solutions.
Still having a mobile redirect problem? Let us know and we’ll help you out.
Works fine for me when testing on iPhone, Blackberry, Opera Mini on an Android smartphone but not on the original Android browser
Hey thanks for the info Peter! I’ll check it out. What tool are you using for checking your mobile sites?
Excellent and vid helped much
You are a ROCKSTAR!!!
Awesome! Glad you found it useful. More to come…
Is there any truth to what I’m reading that I should put this as close to the opening head tag as possible so that no other script execute first?
Sorry for the late reply. I’ve been suffering with the flu, not fun.
For code like this I would include it in my tags. You want it to execute early on in the loading stage. I usually dump the something like Google Analytics code (or code that isn’t super important) into the end of my page in the tags.
Using Javascript for mobile device detection bothers me. I would think it’s taking longer to process. For example, first the page has to load and execute the Javascript, then it has to redirect. Also, we have the potential problem of browsers with scripts turned off.
I find, just using a PHP device detection using server side 301 redirect cuts all of that out.
Hi Clinton thanks for your input. I’m working on a PHP template and will try out a PHP redirect script. Will post my results!
must be folder m not mobile…?
I always put it in a m folder but you could put it in a mobile folder. You would then have to adjust the settings and replace m with mobile.
Ok, where did you get the mobile files from to add to the m folder? Is it from a mobile theme? I am new to all of this so very confused. lol
Yep you need a mobile site to upload to the folder. I sell a simple one, https://mobilewebrockstar.com/simple-mobile-template/.
Steve great tutorial! One question though; how can I have it so the mobile user can go back to the full site via a “View full site” button on my mobile site without them looping back to the mobile? I noticed you have a cookie in the code, could that be used to check to see if the user is coming from the mobile site?
Thanks!!
Hmm let me find out for you. I’ll will post back my solutions in a day or so.
hello i have downloaded the js file and also uploaded the js into my website then i made a subdomain and uploaded the mobile website files into it then i copied the script as you had given into my website
but when i check in my mobile the url doesnt change to m.url why so its not working please help me to sort out my issue
Sure send me your website and I’ll check it out.