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: m.example.com and desktop site at: www.example.com 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 m.example.com is served and then the user types www.example.com 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?

3 Answers 3


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
    header('Location: http://m.example.com/'.$_SERVER['REQUEST_URI']);

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.


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:

https://developers.google.com/webmasters/smartphone-sites/redirects https://developers.google.com/webmasters/smartphone-sites/details

  • Sorry .. I asked only for the redirection logic .. not the technicalities of it.
    – AgA
    Commented Dec 10, 2013 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>:/]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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