I have a mobile site but I need a script to redirect a user to it if they are on a mobile device.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are several ways you can do it, but detecting the screen size is a good way to do that. Note that the screen width is independent of the browser window size.
You can also match specific devices like the iPhone or iPod by the User Agent string.
Important Caveat
While this works, it’s also very annoying to get redirected to the mobile site’s front page when you’re trying to visit a particular page. For example, when you follow a link from Facebook trying to access an article, if the site redirects you to the front page of the mobile version of the site, it’s infuriating and very unhelpful! Chances are, they’ll never try hard enough to find the content.
paulsm4 is correct that using CSS to serve a mobile layout for your site at the same URL instead of redirecting is a much more desirable solution. However if that’s not an option, the next best thing is to redirect to the matching URL for the mobile version of the site.
For example, if you just have a different subdomain for your mobile site, then you can use Javascript to redirect to the correct page of the mobile version: