www.webdeveloper.com
Results 1 to 9 of 9

Thread: Easiest way to redirect from a website to a mobile version of same site?

  1. #1
    Join Date
    Aug 2014
    Posts
    1

    Easiest way to redirect from a website to a mobile version of same site?

    I tried the javascript:
    if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/BlackBerry/i)) || (navigator.userAgent.match(/Opera Mini/i)) || (return navigator.userAgent.match(/IEMobile/i))) {
    location.replace("http://www.jonaskasper.se/treklippare/m");
    }
    but it's not working for me...

    Help, please!

  2. #2
    Join Date
    Mar 2012
    Posts
    1,814
    The easiest way is not to! Seriously, why are you (presumably) building separate mobile and PC sites instead of one responsive site? Use media queries to omit inappropriate content (like large graphics) and to reformat what's left. This not only avoids the need for a redirect, it also simplifies maintenance.

    But that does not answer your query, so here goes: The best way to redirect (for any reason) is to use a PHP redirect before the DOCTYPE declaration:

    Code:
    <?php
    header( 'Location: http://www.example.com/ClientName/index.html' ) ;
    exit;
    ?>
    <!DOCTYPE HTML>
    Notes:

    1. There must not be ANY code or text before this (not even white space).

    2. The file has to be renamed <something>.php instead of <something>.html. E.g. index.php.

  3. #3
    Join Date
    Sep 2014
    Posts
    1
    I also am having trouble with this issue, I tired a different way with JavaScript, regardless it's not working either, so without sounding silly...my index page should be index.php instead of index.html?

  4. #4
    Join Date
    Mar 2012
    Posts
    1,814
    Yes, and enter the code before the doctype as shown. Note: Being PHP, if you try to test it in a browser it won't work. It has to be run on a web server to work!

  5. #5
    Join Date
    Jun 2014
    Location
    Dubai, UAE
    Posts
    43
    You can also do it through htaccess for mobile sites.

  6. #6
    Join Date
    Sep 2014
    Location
    Pakistan
    Posts
    2
    For mobile version first you make sure your theme should be Responsive. you can implement 301 for redirection.

  7. #7
    you can use this code redirect from a website to a mobile version

    <script type="text/javascript">
    <!--
    if (screen.width <= 800) {
    window.location = "http://m.domain.com";
    }
    //-->
    </script>

    You can also use ht file to redirect this based upon visitors.I hope this might work for you.


    Thanks,
    Krish

  8. #8
    Join Date
    Jun 2014
    Posts
    6
    You really want to use responsive HTML and CSS for websites.

  9. #9
    Join Date
    Sep 2014
    Posts
    59
    Using Plugins to redirect

    If you are running a Content Management System (such as WordPress or Joomla), there may be plugins already available that help with handling mobile visitors. The best way to find these plugins is to either search Google or to search the software's website.

    Javascript window.location Method

    Because mobile phones typically have a small screen width, you can redirect visitors to your mobile site if they have a screen width of less than or equal to 800 pixels. You place this code in the head section of your main website code. That way when your site is visited, the javascript will see the browser width and redirect to the appropriate site

    You can use the following code to do this:

    <script type="text/javascript">
    <!--
    if (screen.width <= 800) {
    window.location = "http://m.domain.com";
    }
    //-->
    </script>
    Note! While testing this on a SAMSUNG smart phone, the screen resolution varied based upon how the phone was held. To get the best results, you may have to test with various smart phones.

    Please keep in mind however that if the user does not have javascript enabled, this will not work.

    .htaccess URL rewrite redirects

    You can use a .htaccess redirect to transfer visitors based upon the MIME types the browser supports. For example, if the user's browser accepts mime types that include WML (Wireless Markup Language), then most likely it is a mobile device.

    The code below should be placed in your .htaccess file:

    RewriteEngine On
    # Check for mime types commonly accepted by mobile devices
    RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^ http://m.domain.com%{REQUEST_URI} [R,L]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles