Javascript – How to mobilize all links with http://www.google.com/gwt/x?u=

javascriptmobileprefixredirect

I've trying to make the links on some of my pages "mobilized" with this great service Google Mobilizer: http://www.google.com/gwt/x?u=

Is there a way to prefix all of the links within a div or element, so rather than the link be site.com/link it becomes http://www.google.com/gwt/x?u=site.com/link ?

I'm thinking that something like this might work:

if (window.location == $LINK)
    window.location = "http://www.google.com/gwt/x?u=" + $LINK;

I'm just not sure how to code it. If you know how it's done I would be very thankful!

Best Answer

I think this could be useful for you:

  1. Download the file Mobile_Detect.php at GitHub
  2. Put it on the root of your server
  3. Put this code at the beginning of every page you would redirect

    <?php
    include('Mobile_Detect.php');
    $detect = new Mobile_Detect();
    if ($detect->isMobile()) {header('Location: http://www.google.com/gwt/x?u=http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"],PHP_URL_PATH . '');}
    ?>
    

I tested it with this site and works fine