Grow your CSS skills. Land your dream job.

Redirect users if on desktop to new URL

  • # April 18, 2013 at 6:51 am

    How can i automatically redirect users if they are on a desktop/laptop to a different URL.

    # April 18, 2013 at 6:56 am

    Isn’t it usually done the other way round?

    If they are on mobile send them to a specific mobile site?

    # April 18, 2013 at 7:00 am

    you can use user agent detection with php on the serverside or with javascript on the client side.
    With php the users user agent is stored in

    $_SERVER

    you can go with regex or strpos and testing for the devices you want and redirect with

    header(‘Location: http://example.site.com/‘)

    Respsonsive design is always better though

    # April 18, 2013 at 7:06 am

    @Paulie_D i Guess i could put up the desktop site and redirect to mobile if on mobile. So either way could work.

    @pixelgrid That sounds kinda hard and i don’t know PHP or JavaScript.

    # April 18, 2013 at 7:16 am

    you can find many scrips using php online

    just for the record im posting an .htaccess solution too

    RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$
    RewriteRule ^(.*)$ http://ipad.yourdomain.com [R=301]

    this one is for ipad redirection only

    # April 18, 2013 at 7:19 am

    http://mobiledetect.net/

    Include the Class in your project. If on WordPress that would be in your functions.php file.

    Then just:

    $detect = new Mobile_Detect();

    if ($detect->isMobile()) {
    header(‘Location: http://mobile.site.com/‘)
    }

    If you want the opposite just use the logical operator Not `!`.

    if (!$detect->isMobile()) {
    header(‘Location: http://desktop.site.com/‘)
    }

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".