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

I want the to know the correct desktop to mobile redirection logic. How should a visitor be sent to mobile version or the desktop version of site?

I've a mobile site: and desktop site at: What I'm using now:

  1. First visits will be detected and redirected to mobile or desktop version
  2. Second time onward same session visit to any URL (mobile or desktop) will not be redirected. I mean if first time a is served and then the user types then desktop version will be served from his mobile device

What is the correct way when to transfer a visitor to mobile or the desktop version?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

There's a script called MobileDetect, if checks if the visitor is a user via the browser headers. Requires updating from time to time, but other than that is works very easy:

$detect = new Mobile_Detect();
define(IS_MOBILE_USER,  $detect->isMobile() && !$detect->isTablet() );

if( $onDesktopSite && IS_MOBILE_USER && !isset($_SESSION['ignore_mobileView']) ){
    // Request_uri to keep the user on the actual page, no redirect to home needed

On that site you can add a "view as desktop", which sets $_SESSION['ignore_mobileView'] so the user can remain on the big site (don't forget the possibility to switch back).

As long as there's a session, the site will remember if the user wants the desktop-/mobile version.
You could switch to cookies if you want it stored for a longer period of time.

share|improve this answer

You can use Google's recommendations for mobile sites. Your first step is in compliance with those recommendations but not the second step. You should always redirect based on user agent detection and not session - but allow users the option to choose (usually as part of the footer).

You also need to implement canonical tags (on the mobile pages) and alternate tags (on the desktop pages).

Google's recommendations in details:

share|improve this answer
Sorry .. I asked only for the redirection logic .. not the technicalities of it. –  AgA Dec 10 '13 at 5:43

I actually did this too. You can use modrewrite rules to check the user agent of the incoming device to see if its a mobile device. I use the data from the scripts at:

When the detection takes place, a cookie must be set for a long lifetime (in your case, use 10 years). Its the cookie that you use that will prevent the detection from happening again.

Here's code you can use in your document root's .htaccess file to help you get started. Just replace anything in triangle brackets with correct values as it applies to your site, and feel free to remove NC if you want case sensitivity turned on.

    RewriteCond %{HTTP_COOKIE} !DETECTED=1;?
    RewriteCond %{HTTP_USER_AGENT} ^<insert mobile UA's here>$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.|)<insert main domain here>$ [NC]
    RewriteRule ^<desktop links to redirect>$ http://<insert mobile domain here>/ [NC,R=301,L,CO=DETECTED:1:.<insert main domain here>:<# seconds to keep redirection remembered>:/]
share|improve this answer

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.