Sign up ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Hi how to redirect the user into mobile site when the user accessing from mobile. Say example i have site called Now, a person accessing a website from mobile it should redirect to or

I put some research in google that we can redirect using javascript to get the user agent(browser)

if(mobile browser) {
  //redirect to
} else if(normal browser) {
  //redirect to

or using screen resolution

if(screen resolution < 800 ) {
  //redirect to
} else if(screen resolution > 800) {
  //redirect to

I think It will not work If it is the case of javascript disable.

Can we do this using .htaccess or php stuff?

Is there any standard mechanism to do this?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

use media queries for css

in you css put

@media all and (max-device-width: 1024px) {
... css here

the max-device-width should be the width of the mobile device you want to detect.

on the other hand u can use useragent

in javascript

var isMobile = navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry)/);

if(isMobile) {
   // User-Agent is IPhone, IPod, IPad, Android or BlackBerry

to check the specific user-agent string

if(navigator.userAgent.match(/iPhone/)) {
   // The User-Agent is iPhone

in php this is hwo you do this


check first the useragent of the device

the give the condition

print "Firefox user.";
// process here for firefox browser

in the pregmatch.. you can put into condition 'iPhone','Andriod' etc

share|improve this answer
javascript is not a trusted one, it will not work if it is the case of javascript disabled in mobile browser agent. – iLa Feb 21 '12 at 6:06
edited my post so that u can use PHP – Bert Feb 21 '12 at 6:19
Note that on Opera (Android), the UA string does not contain the "Android" sub-string. – Alix Axel Feb 23 '12 at 11:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.