How to Redirect to a Mobile Version of Your Website

Mobile device users

If you’ve created a mobile version of your website, you’ll need to be sure that you redirect your mobile visitors to your mobile website. For example, if someone visits domain.com on their phone, you’ll need a way to redirect them to m.domain.com (the mobile version of your site created on a subdomain called “m“).

The question then becomes, “How do you automatically send users to the mobile site?”

If you think about how your redirect actually works, in theory, it’s easy to understand- “If the user is using a mobile device, redirect them to the mobile version of the site“. The solution then relies on how to identify your visitor as a mobile user. There are a few different ways to identify a mobile user:

Searching for a new home for your WordPress site? Try InMotion’s WordPress Hosting solutions for secure, optimized servers that are configured for your budget needs.

If you have a mobile-optimized version of your website, then the easiest thing to do to get users to use that is to provide a link that explicitly says that you have a mobile-friendly website. This allows them to manually select to go to the mobile site. It’s not automatic, but it provides a simple solution to the problem. You don’t need extra code, just a well-described link that will catch a user’s eye on your home page.

Use Javascript or jQuery

Javascript can be used to detect mobile devices and there are a lot of different coded solutions that you can find on the internet. It is a well-discussed topic, just make sure that whatever source you view is up-to-date with the current methods that are recommended. The main issue with using code that is looking for specific mobile devices is that it would need to be constantly updated with newer devices.

Use Programming Code to Detect a Mobile Browser

One very convenient source of code for the purpose of detecting mobile browsers is the Detect Mobile Browsers website. It is an open-source provider of scripts for many different languages. The provided code will be able to detect many different user-agents, but it may not catch all of them, so you would have to be familiar with the code and look for updates, or even update it yourself as per your needs.

Use Wireless Universal Resource File (WURFL)

WURFL actually consists of an API and an XML configuration file that give you access to databases that contain information on a wide variety of mobile devices. The advantage of using WURFL is that while the code of the API may go out-of-date, the information about the mobile devices is regularly updated. You can update it periodically and get the most recent list of mobile devices that are being used. You would need to implement the API on your website in order to use it.

As you can see, there are many different possible solutions to have a separate mobile website that is optimized for your users. However, the best solution recommended by the industry is to use responsive design. This allows you to have only ONE set of code for your website. Using responsive design is the ideal solution as it saves time and makes management of your website easier. There’s no need to worry about updating two sites and making sure that both sites are always working.

To learn more about using responsive design, please see our article titled, Introduction: Creating a Responsive Web Design.

294 thoughts on “How to Redirect to a Mobile Version of Your Website

  1. hi – i tried this .htaccess file but doesnt seem to work for me.

     

    i placed it in the root directory of the main “domain.com” – the only thing im thinking is that im testing this on an iphone – is iphone included on those conditions?

    1. The .htaccess method is basically reading what MIME types are accepted by the browser that the visitor is accessing your website through. Mobile devices (including Apple devices) all accept the Wireless Markup Language WML (included in the .htaccess rewrite code), so it in theory should redirect accordingly. Have you tried using Safari from the iPhone in “Private Browsing Mode”? This creates a session in which the cache is temporarily reset (so it’s like clearing your cache without having to lose the “good cache”). Try testing in new private browsing sessions to ensure that the iPhone isn’t just missing the redirect due to Safari’s cache.

  2. Hello Everyone,

    Please can anyone help me to redirect my site www.travelsinternational.co.uk to a mobile version.

    Best Regards,

    Max Miller

    1. The article above does address how to redirect to a mobile site. If you’re having problems with it, then please indicate where you are having an issue. If you require further assistance, then you may need to speak with a developer for help providing the necessary code.

  3. <script>
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    window.location.href=”https://yourblog.blogspot.com/?m=1″;
    }
    </script>

     

    place this snippet just after your haed tag .

  4. hello

     

    i’m stumped.   I created an “m” directory i the root file on the server.   I put the javascript in the head of the desktop website….didn’t work.  In fact wouldn’t come up on a cell.  I the put the code on the mobile page in the head also….no difference.

    What am i doing wrong?

     

    jm

  5. Is there any way to remove the redirect device version in the Domain URl that the user sees? For example I don’t wantxxxx.com/phone 

     

    Cheers

    Chris.

     

    1. Hello, Chris.

      Following this guide will allow you to setup a redirect to an existing Uniform Resource Identifier (URI). However, this article does not cover manipulating URI’s and customizing the way your redirected links appear to your user. As I understand, that would be done via the name and location of the resource you are redirecting to.

  6. How to increase the redirectory speed issues in website to mobilesite .The redirectory speed is  32 seconds……I neeed 3 seconds loaded  to the mobileite

    1. So your site takes 30 seconds to redirect to the mobile version? Can you provide us a link to it so we can look into this further for you?

  7. why would the redirect script disappear from the home page file, I insert the script both home/index file save it then redirects as intended then if an edit changes of the content of the home page the dedirect script isn’t there. I put it back and works again.. any idea how to make perminant…?

    1. Sean, if you are using WordPress or another common content management system( CMS ) any update will overwrite changes you have made to one of the CMS files.

  8. Good afternoon CaseyB and all…

    The script code…

    <script>if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){

    window.location =”https://www.soultrainradio.co.uk/mobile.html”;}</script>

     

    works perfect, but how would I allow users to go back to the original site if they wish to.

     

    Kind regards

    Gary

    1. Most browsers should allow users to choose to access the desktop version instead of the mobile one.

      Alternately, you may be able to create a variable or cookie to remember their choice, and serve the correct option. This will require further development/programming.

      Thank you,
      John-Paul

  9. Good afternoon Arnel,

    Thanks for your response.

    My initial site is responsive at www.soultrainradio.co.uk, but I want phone and tablet users to go to www.soultrainradio.co.uk/mobile.html/ which is a JQuery site I created.

    When I used the JS above, it worked on my IPhone 6, but not a Android.

    Would this be because I left out the / after the mobile.html.

    Kind regards,

    Gary

    1. Gary, when I go to your site on my Android phone I am redirected to your mobile page, and it loads without issue.
      Are you still seeing this issue? Have you tried clearing the cache on the Android phone? If you’re still seeing this issue, what browser and version are you using?

  10. Good afternoon, I have tried the code…

    <script>if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){

    window.location =”https://soultrainradio.co.uk/mobile.html.”;}</script>

    and when I type my web address… www.soultrainradio.co.uk it takes you to the JQuery mobile page on my IPhone.

    My colleague has an android and it does not locate the website and shows a “Website not found error code.”

    Can anyone advise any guidance or resolution.

    Kind regards

    Gary

    1. Hello,

      In reviewing the code, it appears that you might have a stray period in the URL. Make sure that you review your code closely so that you have the correct syntax. Stray punctuation can render the code inoperable. The area I’m talking about is the window location pathname.

      Check out the above comments for different variations on the code. Additionally, please be aware that it would be preferable to have a responsively designed site instead of 2 separate websites (one for mobile). Search engines would prefer the responsively designed site.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  11. I have a website designed in muse. It did have three versions one for desktop and one for tablet and one for phone. This did work breifly but now I can only see the desktop version  on the tablet and phone? Is this the code in the desktop index page being incorrect and not redirecting to the phone or tablet. I have tried refreshing but this doesnt work. So must be either an address issue or in the code?

    This is quite annoying as google are now putting priority to mobile/ responsive sites..

    See code below can anyone help please?

    code from top of html index desktop page:

    <script type=”text/javascript”>var jslang=’EN’;</script>

    <script type=”text/javascript”>

       if(typeof Muse == “undefined”) window.Muse = {}; window.Muse.assets = {“required”:[“jquery-1.8.3.min.js”, “museutils.js”, “webpro.js”, “musewpslideshow.js”, “jquery.museoverlay.js”, “touchswipe.js”, “jquery.musemenu.js”, “jquery.watch.js”, “museredirect.js”, “index.css”], “outOfDate”:[]};

    </script>

    <script src=”/scripts/museredirect.js?4117159434″ type=”text/javascript”></script>

    <script type=”text/javascript”>

       Muse.Redirect.redirect(‘desktop’, ‘/tablet/index.html’, ‘/phone/index.html’, ‘desktop’);

    </script>

     

    1. Hello Nigel,

      Apologies for the issues with your Muse-created website. We cannot troubleshoot the coding issue as it requires Adobe Muse which we do not have. You will probably need to address issue with the Adobe support forums to get the problem resolved.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  12. Hi Christopherm,

    Thanks very much for the response. I think my problem had a lot to do with a plugin, Joomsef and after unstalling, everything is ok now. Thanks again!!!

  13. Hi Guys 

    I have almost completed a project which has normal desktop and a seperate mobile site. I have tried to use the mobile_detect php module.

    When run the site auto detects well and displays the mobile site when needed. But….. I have a link on the mobile site to display the desktop site and this I dont seem to be able to get to work.

    Any recommendations on what the link should have in way of code to work with the php module

     

    Thanks

    Paul

    1. This article only covers the above methods with .htaccess and Javascript, but if you search for PHP in the comments you will see a few suggestions from other users regarding using PHP for redirecting to mobile sites.

  14. I would be most grateful if you could help me find solution to my problem.

    1)  I am on a shared hosting plan with a cPanel account with a fresh Joomla 3.5.1 installation

    2)  I created a subdomain which is m.example.com at the document root which is: /public_html and NOT /public_html/m). And this subdomain is only for the purposes of URL redirection on mobile phones as I have a plugin that will detect and redirect users on mobile phones accordingly.

    3) Now when I try m.example.com on a mobile phone and in a browser with or without the installed plugin for detection and rediretion, it strangely redirects to www.m.example.com which seems very abnormal.

    Please, what can i do to correct it and is this anomally caused by the joomla installation ?

    5) After contacting my host, this is the message they gave me:

    Please check wget checker tool result:

    wget example.com
    Resolving example.com (example.com)… 1xx.xx.xxx.xxx
    Connecting to example.com (example.com)|1xx.xx.xxx.xxx|:80… connected.

    HTTP request sent, awaiting response… 301 Moved Permanently
    Location: https://www.example.com/ [following]

    wget m.example.com
    Resolving m.example.com (m.example.com)… 1xx.xx.xxx.xxx

    Connecting to m.example.com (m.example.com)|1xx.xx.xxx.xxx|:80… connected.

    HTTP request sent, awaiting response… 301 Moved Permanently
    Location: https://www.m.example.com/ [following]

    As you can see, there is a permanent 301 redirect somewhere for both the domain and the subdomain. 

    It was set up neither in cPanel menu nor in .htaccess file. We suppose that it was set up somewhere inside Joomla configuration.

    Many thanks in advance and waiting for your response!

    1. You may want to try doing the redirect with .htaccess or one of the other methods suggested instead of the plugin, just to see if it is the plugin at fault or something related to the domain, or something completely unrelated.

  15. Hi.

    I want to use your js solution. I need to redirect to same path but on mobile site, not to base mobile site. How can I do that?

    I means www.domain.com/dir3/ [redirects to] m.domain.com/dir3/

    Best regards

    1. If I understand your question correctly, that you are redirecting to a mobile version of the content in dir3 on m.domain.com, then, in theory, the above steps as written should still work as expected.

  16. Why not have the mobile version as the default and redirect desktop visitors to a desktop version.  Its unlikely that desktop users would want to view the site as a mobile device.  Mobile users can then follow a simple link from mobile to desktop.  

    1. Hello Brett,

      Thank you for contacting us. Yes, that is definitely a viable options, but I recommend making your website responsive, which means it adjusts and fits to most devices, mobile and desktop.

      Thank you,
      John-Paul

  17. Thank you so much, you saved my life!

    Used the JavaScript code and it works!

    Thank you, thank you, thank you.

    This is why I love the internet!

     

  18. I read very carefully many comments and tried many code script solutions without luck

    My question is simple regarding the redirect

     

    Imagine that I have a website www.company.com

    Its a mobile friendly page BUT i want the user which enters from a smartphone to redirect to www.company.com/blog which shows again many articles, (same site). Ok I’ve done it BUT, when i redirected there, and press MORE to read an article, it redirects me back to www.company.com/blog as before, so I can’t read the article at all. Some problem occurs when a visitor comes lets say from his mobile reading facebook to a specific article on the site, the code will trigger that and will redirect to www.company.com/blog and its frustrating again without reading the article at all. 

     

    There is NO extra url like m.company.com etc, but a same blog page of articles within the website. Haven’t found any solution yet. Everything tried either refreshes again and again the site or the problem I described above.

     

    1. Hello Nikos,

      Sorry for the problem with the site redirect. You need to look at the link that you’re using when clicking on MORE. Whatever that link is, it’s being re-directed when people click on it. You will need to build an EXCEPTION for that link so that your other re-direct doesn’t affect this particle link. We do apologize for no being able to provide code, but we do hope that this points you in the right direction to solve the issue.

      If you have any further questions, please let us know.

      Kindest regards,
      Arnel C.

  19. Mr. JacobIMH

    It will not viewable desktop version on my mobile while i am pasting that code…. what i want to do plzzz help me 

    1. Hello Gobi Jmp,

      Sorry for the problem with the code. Can you please provide detail on what you have done to add the code that Jacob has provided? We’re not sure exactly what you have done and what kind of problem that you are having. If you can please give us details and a URL, then we can investigate further.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  20. In what file do you have to place the code? Where is the head section of the website code? P.S. I’m using wordpress.

    1. Since you are using WordPress, your site should already be mobile friendly. This is because the latest versions of WordPRess are “Responsive” by default.

      If you still want to redirect, I recommend using a WordPress plugin instead of hard coding a redirect.

      Here is a link to our full guide on Adding HTML to a WordPress Page/Post.

  21. hi there, anyone can help me? I have to redirect my old post that use www.mydomain.com to www.mydomain.com/dir

    I use .htacess but some of my post use mobile url like mydomain.net/xxxx.HTML?m=1 I tried to make some code in .htacess, but when I tested It was work, I can redirect use plugin in wordpress, but the problem is I plan didn’t install wordpress in my main domain, so the one and only I should use .htacess. please help. 

    1. Hello Michael,

      It’s not really a major problem but if you don’t like your developer knowing things such as this then you could always find another developer.

      Best Regards,
      TJ Edens

  22. The only criteria should be screen size. Not the platform. On a large tablet, you want to show the full-featured site. On a PC with small screen (a phone, a netbook, or a wide screen in portrait mode) a narrow-screen setup is better.

  23. Question – if a user access a page https://mywebsite.com is redirected to https://m.mywebsite.com … but if a user access another page https://mywebsite.com/example how can be redirect to page https://m.mywebsite.com/example ?

    Thank you!

    1. Hello George,

      Thank you for contacting us. The .htaccess option above may be a possibility, have you tried any of the options described above?

      Also, keep in mind if you are using CMS (such as WordPress, Joomla, Drupal) it is recommended to use a plugin, or extension.

      Thank you,
      John-Paul

  24. please correct as i want to redirect desktop website for mobile user to mobile site…plz need ur help …

    .htacces (file)

    Options +FollowSymLinks
    RewriteEngine on
    #Local
    #RewriteBase /hyundai-saudiarabia/
    #Server
    #Check for mime types commonly accepted by mobile devices
    RewriteBase /

     
     RewriteRule ^arabic/ar/cars/(.*)$ /arabic/ar/index.php?car_name=$1 [NC]
     RewriteRule ^arabic/ar/commercial/(.*)$ /arabic/ar/index.php?car_name=$1 [NC]
      RewriteRule ^arabic/ar/suv/(.*)$ /arabic/ar/index.php?car_name=$1 [NC]

    RewriteRule ^en/cars/(.*)$ en/index.php?car_name=$1 [NC]
    RewriteRule ^en/commercial/(.*)$ en/index.php?car_name=$1 [NC]
    RewriteRule ^en/suv/(.*)$ en/index.php?car_name=$1 [NC]

     
    RewriteCond %{HTTP_ACCEPT} “text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml” [NC]
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule ^ https://www.hyundairetail.com%{REQUEST_URI} [R,L]

    1. Hello Raj,

      Sorry for the problem with your .htaccess file. Providing programming support is unfortunately outside the scope of our support. If you’re unable to resolve your .htaccess code, then it’s recommended that you speak with a developer/programmer concerning the problem. We did review your code and did not see anything that was obviously wrong. You may want to consider using the Javascript solution as opposed to the .htaccess option. Apologies again that we can’t give you a direct answer.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  25. i want to know to the name to use in saving the .htaccess file

    .htaccess#sthash.oIRR6up4.dpuf
    .htaccess#sthash.oIRR6up4.dpuf
    1. Hello collins,

      Thank you for contacting us. You should just save the changes to the .htaccess file without renaming it. This means it should remain named .htaccess.

      Thank you,
      John-Paul

    1. Hello meenakshi,

      You would place your javascript at the top of the pages you wish to redirect.

      Kindest Regards,
      Scott M

    2. Hello Debbie,

      Thanks for the question about adjusting the script to accommodate small monitors. We’re not sure what monitor sizes you’re trying to support. Unfortunately, the best way to determine would be to experiment with the sizes and see if it works for all the different sizes you need to support. We’ve tested only as low as 800 pixels, and this was done with only the few different monitor sizes that we had available to us.

      I have also seen this kind of issue handled through CSS. Check out this post as it may provide another solution for you.

      Apologies that I can’t provide you a direct answer on this issue. I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

  26. Thanks, TJEdens,

    It did work on the ipad as well as the iphone after I changed the 800 to 1024, however, if 1024 worked I am certain 1050 would too.

     

    The only issue I am having now is that some monitors in the building are still pretty small.  Would like to how low I could take the number to match the largest ipad screen to cover most devices without taking the chance that a monitor might still be small enough as well.

     

    #getwiththetimeswhostillhasasmallmontior

  27. I used the original code posted above to redirect to our mobile site and it worked perfect for the iphone, however not the ipad.  Changing the screen width to 1024 made it work on both.

     

    <script type=”text/javascript”>
      <!–
      if (screen.width <= 1024) {
        window.location = “https://m.domain.com”;
      }
      //–>
    </script>

    1. Hello Debbie,

      So it works on your iPhone but not the iPad. Try lowering the number to like 1050 and see if it works on the iPad then.

      Best Regards,
      TJ Edens

  28. I am not using a CMS … it’s an html site.

     

    When it stalls, it just goes to a blank screen and says domain.com at the top. I have uploaded it to the back of my site to see how it works but wasn’t sure if they had to purchase the m.domain first. Basically, that’s just like you would your normal domain, correct? It can all host together. Sorry to sound ignorant but I haven’t done mobile as of yet. It’s my first go at it.

    The index url is https://talmagedesign.com/schwartz/ where it is housed now while I’m figuring this out. Thanks.

    1. Hello taldesign,

      If it is an html site, then making it mobile friendly instead of having a separate mobile version is much more desirable, both for design-maintenance as well as Google. However if you wish to use a separate m.domain.com then you simply need to create a subdomain of m.domain.com. It is part of the man domain so it does not need to be registered separately. It does need to be created on the server however as it will need to point to its specific directory.

      Once all that is in place (along maybe with ‘hello world’ test index page in the m.domain.com) you should see it work correctly.

      Kindest Regards,
      Scott M

  29. Hi there. Thanks for the tips! This is my first time doing a site redirect. My client has not yet obtained the m.domain. I’m trying to test using a regular domain with the accurate file name but am not getting anywhere. It stalls going to domain.com. Thoughts?

    1. Hello Taldesign,

      Thank you for contacting us. If you are using a CMS (such as WordPress, Joomla, Drupal, etc.) I recommend using a plugin to create the redirect. This reduces the chances of existing rules interfering with the one you are adding to your .htaccess.

      We are happy to help you troubleshoot further, but will need some additional information. Do you get an error when it stalls?

      How are you testing, without the m.domain yet?

      Please include any additional information that will help us replicate and test your site.

      Thank you,
      John-Paul

  30. www.dailygeopakistan.com 

    on laptop and computer its ok but on mobile and tablets its go to one side and not run good kindly provide any solution i alreasy used your codes 

     

    1. Hello Gill,

      Apologies for the problem with your website. We unfortunately do not provide assistance for re-coding your site for mobile use. You may want to consult with a developer or website designer for further help. Check out Google’s guide for Mobile sites for an example of what’s needed to make your website mobile-compatible.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  31. I know this is old and I am sure that the link issue was figured out, but why didn’t someone suggest using cookies and saving a value that can be used to differentiate between desktop and mobile?

  32. Hello!

    thanks for sharing valuable information, but I mixed up a bit. I have installed redirection code on my main website  example.com to mobile version of it https://adheap.example.com/

    I tried to add code snipet to my mobile version in dashboard. But still it doesn’t redirect to main website from mobile version.

    Please, help

     

    Nataliya

    1. Hello Nataliya,

      Thank you for contacting us. As a test, have you tried adding the code to the top of your .htaccess file?

      Also, as a test, try renaming your .htaccess file, then creating a new one with just the rule you are adding.

      Thank you,
      John-Paul

  33. Hi,

    This may be a stupid question(s), so apologies. But do you create a whole new file for your .m code, or can this be in with your code for your main site? Also, do you have to buy, m.domain.com, or does that come with www.domain.com. Finally, if you do have to create different files for your mobile site, do they share the same hosting and folder within your hosting, as your normal site. 

    Many thanks

    1. Hello Ian,

      It’s a good question. The answer is – “it depends…” If your site is small and you have one hosting service with only a little traffic, then you can keep everything in one place. If you have a lot of people hitting your site, or your site requires a more robust service for reasons of function or support of an online application, then you may need to have a different location for your mobile site (or your main site).

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

    2. Hello Ian,

      It’s a good question. The answer is – “it depends…” If your site is small and you have one hosting service with only a little traffic, then you can keep everything in one place. If you have a lot of people hitting your site, or your site requires a more robust service for reasons of function or support of an online application, then you may need to have a different location for your mobile site (or your main site).

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      TJ Edens

  34. I have a website chattercreative.com which points to a mobile version which was created at 414px. Google wants a version for small screens… So I made a version at 320px. But can’t get the Google mobile bot to detect it. Any help that I could get from the community would be great:)

    https://chattercreative.com/mobile/320/320_mobile.html

     

    <script language=”javascript” type=”text/javascript”>

    <!–

        if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Android/i)))

    {location.href=(“https://chattercreative.mobi”);}

    –>

        </script>

        

    1. Hello Matthew,

      I would suggest using this as it will apply to all mobile devices.

      script type=”text/javascript”

      if (screen.width <= 699) { location.href=("https://chattercreative.mobi"); }/scriptBest Regards, TJ Edens

  35. Can my load balancer will be able to identify, if there is any request comes from a mobile or tab so that will route to a different web server.

    Regards

    -Amit

    1. Hello Amit,

      These days, responsive websites are created for purpose of handling mobile or non-mobile websites. This prevents the development of separate code just for a mobile site. It’s not that this kind of thing can’t be done, but you will require a programmer/developer if you are unable to put the code together yourself. This is unfortunately not something we can provide a direct solution for you. You can use the code provided in our tutorials, but modification of it for your purposes will require a developer or programmer.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

    1. If a device has a resolution larger than 800, then it will use the normal site. If you wish for all mobile devices to display the mobile site then you will need to go by device type instead of resolution. There are many examples above for that as well.

      Kindest Regards,
      Scott M

  36. <script>

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

    if(document.URL !=”mob.error.htm”)

    {

    window.location =”mob.error.htm”;

    }

    }

    </script>

     

    There is the code that belongs in the page (substituting your mobile page file name or URL for mob.error.htm)

     

    Mob = mobile

    error.htm = bad link I can search for on my drive to make sure that the code has been changed for all pages.

     

    This code belongs in the mobile-ready page:

    <meta name=viewport content=”width=device-width, initial-scale=1″>

    1. Hello Scott,
      Thanks for your reply. I’ve searched in my server public root directory, but didn’t get .htaccess file. But I tried .htaccess process by creating a .htacces file and inserting code. That code works but now I am getting another problem. The problem is I have another two domains in same server and all other domains are redirecting on 1 single mobile site because of root .htaccess file.

    2. Hello all,
      I am using java script code to redirect my main site to mobile version site, code is:

      This code is working perfect with all devices including Iphone (All models including IPhone 6+) IPad, IPad mini, mostly samsung devices, tablets and all android smart phones. But problem is this code is not working on Blackberry devices. I am using Google developer’s mobile device mode to test all devices but this snippet is not working with Blackberry Playbook and Z30. So far I’ve tested all default devices provided by Google Developer’s tools but only giving error on blackberry devices.
      My website is : www.elementomosaic.com
      Please look into it and tell me where I am making mistake.
      (P.S: Sorry for bad english)

    3. Hello Rahul4450,

      I used your code to test using the Chrome mobile device emulator and the BlackBerry was indeed the one that failed. Unfortunately I was unable to get the BlackBerry to work even with testing different things. Other device emulator sites did not work as they only simulate screen size and do not function as the device, thus not allowing the redirect to work on any device.

      Kindest Regards,
      Scott M

  37. Hello all,

    I come here through search engine and got some new things, but still i am very confused between multiple solutions and codes. The is I have GoDaddy hosting account with multiple domain hosting, and hosted many sites (4 sites) in same hosting account. Now I want to redirect my 1 of old website to mobile version but I don’t want to edit html code as there are many pages. So may be .htaccess is good idea. So can you provide me a good .htaccess code so that i can redirect all mobile, IPad, IPhone and tablet devices to mobile version using user agent. Currently I am using :

    <script type=”text/javascript”>

      <!–

      if (screen.width <= 800) {

        window.location = “https://m.domain.com”;

      }

      //–>

    </script>

    But this code is not accurate with all latest devices including Kindle Fire and many more.

    So can anyone provide me a quality code for this..

     

    1. Hello rahul,

      The code you provided is javascript so if you want to use an htaccess file then it is not the right code for you. Just make sure the site has an htaccess file in its root and then you can follow the guide above to insert code to redirect your mobile site. Be sure to read all of the article so you do not miss any important details.

      Kindest Regards,
      Scott M

  38. Hi all,

    nice site, thanks for the info I’ve found here.

    I don’t know if someone already wrote a solution like mine (didn’t read all), but I’ve done in this way:

    Thanks to “Andrew Allen West” I made the same PHP check, getting a boolean variable $isMobile.

    Then, since I create the html dynamically via PHP loading files and replacing variables (mainly for multi language reasons), I load the right CSS file according to the $isMobile flag.

    $headerFile = 'global/head.html';
    if($isMobile)
    {
    $headerFile = 'global/head_m.html';
    }

    And that’s it, same code for all, different style for mobiles.

    Ciao

  39. Switch between mobile site & desktop site

    If you have subdomain for mobile users like- www.m.example.com and for desktop users you have domain like— www.example.com. 
    To switch between the websites, you need to determine the device width (which is usually <=800 px for mobile phones) and show them the appropriate site i.e. www.m.example.com for mobile users and www.example.com for desktop users.
    To accomplish this, write this code in the first page which the user see when coming to your site (i.e. desktop version)
     
    <script type=”text/javascript”>
            var getStr = window.location.search.substr(1);
            var getArray = getStr.split (“&”);
            var get = {};
            for ( var i = 0; i < getArray.length; i++) {
            var tmpArray = getArray[i].split(“=”);
            get[tmpArray[0]] = tmpArray[1];
    }
     
           if (get.m == null) {
                 if (screen.width <=600){     <!—please don’t write px after the number—>
                      window.location=“m.example.com”; <!—both absolute and relative path will do —>
                 }
          }
    </script> 
     
    And write this code in the mobile version of the site—
    <a href=“example.com?m=no”>Go to desktop site</a>
     
    By this code, m will become “no” and will not check the device width. Hence it will display the website as it is i.e. the desktop version.
    when the user comes for first time on your site— the script will check the device width (because m is null), if it is less than equal to 600, it will take you to the mobile version i.e. m.example.com. If the width is more it will stay on the same html file i.e. the desktop version.
    1. Hello Allen,

      Apologies for the frustration, but you would be best served in getting a developer/programmer for this issue. We don’t provide coding support of this nature.

      Regards,
      Arnel C.

  40. Hello,

    I’ve set up a mobile version of my company website and I’m trying to sort out the redirects and it’s driving me insane! I can redirect to mobile, no problem, using the javascript at the top. However, it’s getting back to the desktop version that’s a pain.

    I tried the yes/no code and it works, kind of, except now my iPhone keeps going to the desktop version of my index page. The problem is, when I’m on that desktop index page and click any link on my site, it redirects to the mobile page. I tried adding the yes/no script to another page and clicking that but it still redirects.

    Is there any way to add a session cookie or anthing, that will retain the yes/no preference?

    I had a go at the .htaccess file, copying in the code above and removing the javascript, but nothing happened. It didn’t reditrect at all.

    The site is https://clocktowercreative.co.uk and the mobile version is https://mobile.clocktowercreative.co.uk.

     

    Cheers!

  41. So if I cange the last line in the .htaccess from:

    RewriteRule ^ https://m.usnewspapers.com%{REQUEST_URI} [R,L]

    To: RewriteRule ^ https://usnewspapers.com/m%{REQUEST_URI} [R,L]

    It will work fine?

    Also thanks for the SEO tips. As for the rsponsive site, all of our newer websites are responsive. Unfortunately this is a very old website I came and met in the company. I suggested to my boss that we redesign it but he's feeling reluntant cause the site is doing really good on search engines. Since the entire site is html other than the contact form, redesigning it would means redesigning hundreds of pages, even thousands. And he doesn't want to redevelop it cause he thinks it might loss it Search Engines place and might take a while to get back at the position again. So I'm just going with the flow by create a mobile version of the site as he suggested.

    Thanks
  42. Hi Scott, I working on an html website, I don’t want to use the javascript version of your code cause than I will have to put it in every single page of the site. It’s like over 100 pages on this website, so I want to use your .htaccess code in my site directory instead. But I don’t want to create a subdomain m.usnewspapers.com for SEO purposes. What would be the right way to put in usnewspapers.com/m/ in the htaccess code?

    Thanks

    1. Hello Prince,

      I was able to get my own test site to go to example.com/m simply by changing the redirect line from m.example.com to example.com/m

      Just a quick fyi for your SEO situation. Google has said they do not penalize m. domains as duplicate content for websites. They understand the older method of having a mobile site. You would benefit the most by having a responsive site, however.

      Kindest Regards,
      Scott M

  43. Anyone still having problems with the link back to desktop site, this code worked for me. It was easy to implement and you can read more about it’s installation at :https://www.soundfeelings.com/free/best-mobile-redirect-javascript

    The code: Basically you just replace the red and green urls with your own, then put the code in the HEAD section of the page.

    <script type=”text/javascript”>
    if (screen.width < 574) {
     var ref = document.referrer;
     var urls = new Array(“
    https://www.mymainsite.com“,”https://m.mymobilesite.com“);
     var n = ref.match(urls[0]);
     var m = ref.match(urls[1]);
     if ((m!==null) || (n!==null)) {
     stop;
     }
     else if (ref==”) {
     var r = confirm(“Small Display is Detected.\nClick \”OK\” for MOBILE SITE.”);
     if (r==true) {
      window.location = “
    https://m.mymobilesite.com“;
      }
      else {
      stop ;
     }
     }
     else
     {
     window.location = “
    https://m.mymobilesite.com“;
     } 
    }
    </script>

  44. hi i Need help

    I added code to my site Excellent work but if user whont to back to full site Not possible.

    however i saw this question is asked but i Not success usere cnot go to full site

     

    Please Someone Can write full code (code for full site and code for mobile Button to back fukk site)

    this code in my full site

    type=”text/javascript”>

      <!–

      if (screen.width <= 800) {

        window.location = “https://www.mobile.com”;

      }

     if (get.m = “no”) {

    window.location =”https://www.main.com”;

    }

      //–>

    </script>

    in my mobile site i heve this Button code  to back full site

    https://example.com/?m=no

    Thank you

    1. Hello idan,

      Unfortunately we do not provide code solutions, but the examples we have above have been tested to work. You will want to use standard code troubleshooting procedures in order to correct the issue as well as testing as you go along.

      Kindest Regards,
      Scott M

  45. When linking back the main site using the ?m=no, it only works for one page. Once I am on the main site any link will redirect back to M. site, page 1. Like a frustrating loop or a peice of gum that keeps moving from finger to thumb, when trying to flick it off. (just some sillyness to break the tension)

    Is this a possiblity…
    I create a new subdomain, let’s say x.example.com.
    In this sub, I make an exact copy of example.com.
    I use the robot txt file to tell search engines NOT to index. Like an invisible copy.
    This would make it easy to deal with the loop issue, while still having a main site that is indexed and a mobile site (m.example.com), that will also get indexed.

    While a bit of a hack (i guess), I wonder will it work. If robot is set to dis-allow, it should not affect SEO, correct or am I missing something?

    Thnaks

    1. Hello Jon,

      You can try that if you wish, but I would recommend against it. Check it out with Google’s mobile friendly test tool. If it’s okay, then you’ll know soon enough. Google provides a lot information that would helpful in your efforts to make your site more mobile friendly.

      Regards,
      Arnel C.

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

    WORKED FOR ME!!! THANK YOU SO MUCH!!! I LOVE YOU!!!
  47. I want to use the htaccess method of redirect to mobile, but only from my entry index. The idea is I want the mobile site to be able to use the main sites contact form. How do I alter the code?

    1. Hi, I have found this thread very useful and so decided to join. I have used the Javascript window.location Method as shown above and all is working well on the two sites I have added it to.
      I feel I have been forced to create two mobile sites because of the ‘threat’ of Google penalising them on mobile search results.

      Before I added the javascript above, I had put in the page header the google annotation as they advised:

      Desktop Page:

      Corresponding mobile page:

      Just before I had added the javascript to one of my sites, I searched for it on Google with my iphone and saw the mobile version of the site already in the search results.

      Since I created my mobile sites for the sole reason of not getting penalised in Google, I now wonder what the panic was about with the redirecting. Google seems to have done the job for me. I’m wondering why they are so concerned about re-directing using what ever method when clearly they have done the job themselves through the search results

    2. Hello Nels,

      The .htaccess method will attempt to run with every page, so you would need to attempt to modify the code to specifically address the index page. In that case you may want to have the mobile index page in the same folder so that the link paths could remain the same. Perhaps you would name the mobile index page as index2.php. as to specific code, we are happy to answer specific questions you have but are not able to custom build a solution.

      Kindest Regards,
      Scott M

    3. Hello Karen,

      Google’s new algorithm change will be penalizing sites that are not mobile friendly. This means the main version of your site would be penalized as it is not ‘responsive’. Google ultimately does not want for sites to have a regular and a mobile version, but a single version that works for all devices. Those are the responsive ones. The 2-version method of web design is already outdated and was a short lived response to building sites before responsive design came about.

      The good news is, your main site will not be penalized for searches made on a PC, just for mobile searches. However Google may still hit both sites for duplicate content, since they are two independent sites with the same information. We urge all site owners to try and build a future where they have responsive sites as they are easier to maintain once built and can avoid silly Google penalties.

      Kindest Regards,
      Scott M

    4. Hello Jon,

      Yes, if the code is correct, it should run for each page and come up with the same result. In our testing, we hav this behavior. We would need to see links to the misbehaving site or pages so we could see if there is anything we can assist with.

      Kindest Regards,
      Scott M

    5. Hello Scott,
      I did read here: https://developers.google.com/webmasters/mobile-sites/mobile-seo/configurations/separate-urls?hl=en that having a separate mobile site was ok with Google. The rel=”alternate tag was the recommendation from Google so that the association was made with the main site so should have no duplicate content issues. I have added and associated the mobile sites in analytics and webmaster tools 🙂

    6. Hello Karen,

      Perfect! Always follow the recommendations for Google as they are responsible for most of the traffic. Keep one eye to the future for responsive sites however as they seem to be moving towards that direction.

      Kindest Regards,
      Scott M

  48. The code is in the head sec of all pages.

    What the issue is, is thei.

    Once they get to main site, via a link that says ‘Full Site’, any other links clicked there, bring them back to Mobile Test Page (the 1st mobile page)

    Once they choose main site, they should be able to stay there, correct?

    Thanks for such a quick reply.

  49. What a great forum.

    I too, need to redirect from a main site to a mobile site.

    I tried the JS code that detects screen size. This works.

    However, once on the mobile site, I can link to main site but only to the page specified, in this case index.html.

    Once folks are on the mian site on the phone, when they click any link on the site, the mobile test site pops up again.

    The site is https://green-mfg.com

    With all the posts above, it is starting to get confusing as to which is the best and which to use. So, I was wondering if you can address the main site viewing issue in the phone, while providing the best code to use. The more examples provided the better. Like a sumation of all of the above, while weeding out what may not be needed. I hope this is understandable.

    Thanks

    Jon

     

    1. Hello,

      The js code will need to be on every page in order to stay consistent with the device. Did you have any specific questions regarding that?

      Kindest Regards,
      Scott M

  50. My site asks the user’s device if it has Flash, and if not it re-directs to an HTML5 site that is optimized for mobile devices.  It works like a dream except for one problem: as soon as I go live with the re-direct version, my Adsense revenue falls by a hefty 25%.  I can’t figure out why this is so – help!

    1. Hello Tim,

      Thank you for contacting us. It is difficult to say without access to your Adsense information. I recommend reviewing your stats in Adsense, or Google Analytics, as you should be able to see the source of the traffic, and compare before and after.

      Thank you,
      John-Paul

  51. Hi,

     

    Thanks for the work you are putting into this. I am sure there are many more people out there who are grateful than are posting. 

     

    I am using the javascript to redirect which is working perfect. The problem is with the link back (again). It is working fine on the testing server on my own machine but when put on-line it does not work. Here is the code. Do you have any ideas why it is failing?

    This is in the HEAD of the desktop site:

    <script type=”text/javascript”>

     

    <!–

     

            var getStr = window.location.search.substr(1);

     

            var getArray = getStr.split (“&”);

     

            var get = {};

     

     

     

            for ( var i = 0; i < getArray.length; i++) {

     

            var tmpArray = getArray[i].split(“=”);

     

            get[tmpArray[0]] = tmpArray[1];

     

            }

     

     

     

           if (get.m == “yes”) {

     

                    window.location = “https://m.norfolkbroadsboathire.biz”

     

            }

     

            else if (get.m == “no”) {

     

                    window.location = “https://www.norfolkbroadsboathire.biz”;

     

            }

     

     

     

     //–>

     

    </script>

     

    This is in the head of the mobile site:

       <a href=”https://www.norfolkbroadsboathire.biz/?m=no”><em>This Page on Desktop Site</em></a>

    1. Hello David,

      I tested your code on a server here and it is working correctly. You may want to contact your hosting support to see if they have any specific server setup preventing this from occurring.
      Kindest Regards,
      Scott M

  52. Ok, my brain’s officially exploding now.

    I’m a small business owner and built my own site that looks very professional and I like it, as do my customers. Because of the google changes, I’ve been looking at creating a mobile site (m.xxx.com) and redirecting from my classic site. From what I read here, that now sounds like a bad idea.

    First off, let me say that I cannot afford to pay to have a responsive site built, and the skill level to create one is beyond me (unless you can point to a tutorial that actually works).

    So…..if I’m building a mobile site that redirects from my classic site, my understanding is that this effectively splits my seo ‘points’ between the two sites??? If so, what is the point in creating the mobile site to begin with?

    Does it make sense to even bother with the mobile site??? 

    1. Hello Jaime,

      Your concerns are well placed. Basically, Google is rewarding the new technology over the older way of doing things. Your SEO will be impacted in such a way that if there is a competitor for your subject matter and THEY have a responsive site, then they will be ranked higher. It makes no sense to build a mobile site if one of the biggest search engines is making responsive websites a priority. It does sound like it would be difficult to build one, but really, you simply need to isolate the elements in your website that would need to be moved around as the screen shrinks. If you can’t do it yourself, then it would be wise to seek the expertise to have it done for you.

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Arnel C.

    2. Hello Jaime,

      I wanted to add a reference for you to review for more information. Check out this Google official documentation on Mobile SEO.

      Regards,
      Arnel C.

    3. Hello Ally,

      I’m sorry that you’re having problems with the mobile code. You need to think of the detection and re-direct code in terms of how your viewers are viewing your website. What do they see first? Whatever is appearing first is where the detection code and the re-direct code should located. If you are having trouble with figuring it out within your website code, then you should employ the services of a web developer/programmer. We do provide development services, but their main focus would be build your entire website (InMotion Web Design). Providing code support is unfortunately outside of our scope, we do try to point you in the right direction.

      In terms of Google’s upcoming update, you will need to consider that Google is placing Responsive Design ahead of websites that use mobile re-directs (Google and Mobile SEO) . If you have a highly ranked site, that’s great, though if there is a competitor who is ranked close to you and their site is using responsive design, then they may be ranked above you after the update.

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Kindest regards,
      Arnel C.

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

  53. Hi there!

    I hope someone can assist me in this conflict.

    I am in need of help for redirecting users to my mobile site.

    I have tried much of the code recommended on this page and various other pages online: 

    .htaccess, Javascript, HTML redirect, etc.

    None of them seem to work for some reason, so I am not sure what I am doing wrong. I am following directions as said. I am also placing each respective snippets of code in the areas specified when I try each mobile redirect suggestion (separately). Within head tag, .htaccess file, etc.

    Again, I am not doing these all at once, I am saying that I’ve tried different methods separately to see which could work.

    Each time I check on my cell to see if a code method works, I still get my desktop site, and not a redirect. 

    My destktop website is: https://axelbloom.com/index.php

    and my mobile website is: https://www.mobile-axelbloom.com/

    The mobile site is an external mobile site and not a subdomain. I’ve designed the mobile site on Squarespace.

    Also, if push comes to shove and I can’t get this redirect to work so that my desktop stays visible to desktop and tablet users, and I need to just replace my desktop site completely with the mobile site, how will this affect my SEO results in Google? Will it considerably lower or eradicate my ranking? Right now, the ranking is pretty high for the site, number 1 in search for related keywords and search terms.

    Thanks in advance for any help. It is much appreciated.

  54. Hi There!  The htaccess script is working great for me, but how do I turn it around to ensure nobody accidentally lands on a mobile page using a computer. With the script, it only ensures that people using mobile don’t accidentally land on the desktop version. If someone shares a direct link to a page on the mobile site and it loads in a desktop browser, it looks terrible!  How do I make sure the desktop always loads on desktop, and the mobile always loads on mobile, and never the other way around.

    1. Hello Alyssa,

      If you read near the beginning of the article, there is a Javascript option there that detects the size of the screen and auto-redirects. That’s basically what you would need to do if you want it to automatically go to a specific site based on screen size. Otherwise, you should consider making the site with responsive design.

      Regards,
      Arnel C.

  55. Jeff.. Everything is working great on the redirect EXCEPT.. when i use the ?m=no to go back to my main site it only lets me browse the page it goes back to then when i try to go to another site on the main website it redirects back to the mobile site.  Is there a way to get around this??

  56. Got my code working great.. only problem is my link back the main site using the ?m=no only works for one page then the next page I click on it sends me back to the main site.  Is there a way to retain that m?=no for all pages once you click it?

  57.  

    The help I received here on how to re-direct was excellent. I now use code to direct mobile uses from my “classic” desktop website pages to my dedicated mobile website pages and vise versa. It works really well. (Google’s mobile testing tool gave me an AWSOME ranking)  I like the fact that I can be creative with both websites and don’t have to follow the “cookie cutter” format of a dynamic website AND I didn’t have to re-do all my hard work I put into my “classic” website. 

     

    Now with Google set to punish rankings of non-mobile friendly websites I can see how this technique is going to be very helpful to small businesses who can’t afford to have their old non-mobile websites rebuilt.  What if a website designer just builds a “mobile friendly front end”, i.e. a second dedicated mobile website at 320 px wide?  Google won’t show the “classic” non-mobile website in searches using mobile devices.  Google will only see the “new” second mobile website in mobile searches and the business won’t lose any traffic because the “classic” web site is not mobile friendly.  Then in the mobile dedicated website which will be seen when people are searching with larger devices like desktop computers as well as mobile devices, you add some code that says if a device with a larger screen is searching you re-direct that device to the “classic” website. 

     

    Mobile websites can be smaller and don’t need to have as much information as the “classic” website and you can always link it to the “classic” website if there is specific detailed information a user might want.  I think in many cases there are distinct user needs between those using mobile devices and those using larger devices.  Building a small mobile front end website will be easier/cheaper than a whole over haul of the complete website. For a small business or organization this is going to really help them out by saving them money.   For web designers this could be a competitive advantage to beating out the competition with lower costs.

     

    Cheers,

    Ted

     

    1. Ted,

      Thanks for your comment. In general, the concept of having a separate mobile site and a “main” site for larger viewing displays is starting to become an older, obsolete concept in the realm of website designers. Newer developed web sites are being developed using responsive design. This allows the user to have a single website that will automatically adapt to the display of the viewer, without using a re-direct. Google is in essence trying to give precedence those who have made the effort to accommodate the many different devices that can view the web in this day and age. We do have a few resources about responsive design, but if you do a search, you’ll find an exhaustive amount of material that will help to explain the use of responsive web design in the industry today.

      If you have any further questions or comments, please let us know.

      Kindest regards,
      Arnel C.

  58. Yes, this is working as a mobile redirect with https://www.m.example.com/index.php?dogs=yes as the loop back to desktop site. There I have a @media css button specified for mobiles only with a direct link back to the page on the mobile site.

    All seems to be working well.

    I believe I am detecting any varient of ipad, iphone and android browsers.Is there any additional HTTP USER AGENT strings you recommend I use to catch more mobile visitors?

    Also wanted to share a .htaccess file which is working well for me so others may beable to use it.

     

  59. Ok, so as suggested I’ve been back and written my .htaccess file as the following:

    .htaccess in root folder of desktop site:

    #####

    Options +FollowSymlinks
    RewriteEngine on
    # Check to see if we have already set the cookie.
    RewriteCond %{HTTP_COOKIE} OverRideRedirect=yes [NC]
    RewriteRule ^(.*)$ – [S=8]

    # Check if this is the mobile to desktop query string — if so SET the Cookie and SKIP the next 6 Rules.

    RewriteCond %{QUERY_STRING} dogs=yes
    RewriteRule ^(.*)$ – [CO=OverRideRedirect:yes:.example.com:30,S=6]

    RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$ [NC]
    RewriteRule ^(.*)$ https://m.example.com/$1 [R=302]

    RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ [NC]
    RewriteRule ^(.*)$ https://m.example.com/$1 [R=302]

    RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$ [NC]
    RewriteRule ^(.*)$ https://m.example.com/$1 [R=302,L]

    #####

    The link back from the mobile site is then placed on each page in this format:

    https://www.m.example.com/index.php?dogs=yes

    #####

    So detecting any varient of ipad, iphone and android browsers.

    Can anyone see a problem with this .htaccess?

    I can’t get it to fail.

    Is there and other mobile user agents you think I should add?

    Thanks

    1. Hello Harsha,

      Thank you for contacting us about a 500 error.

      Since an existing rule is most likley interfering with the rule you are adding, you will have to review the code of your .htaccess files.

      If you are using a CMS such as WordPress, Joomla, Drupal, or Concrete5, you may have to use a 3rd party extension, or plugin.

      Thank you,
      John-Paul

  60. Hello Jacob, thank you for this manual. I was able to detect and redirect my site to the mobile version, HOWEVER I wasn’t able to allow the user to go to the desktop version if he so wanted. I copied the code you provided in both the Desktop version and Mobile version of my site, but whenever I click on my desktop version link I am taken to the mobile version. Could you please ellaborate how to override the mobile version if the user wants to see the desktop version on his mobile?

    Thank you!

    1. Hello,

      You should just have to make the desktop link this: https://example.com/index.php?m=no

      Best Regards,
      TJ Edens

  61. Scott – thank you SO much. I really appreciate your willingness to help me; especially helping make it so clear for someone that’s not a developer. Gold star for you!! 🙂

    P.S. I’ll pay it forward soon and send some positive energy out there as gratitude for you. Thanks again!!

  62. I have two very basic HTML sites, one of which is optimized for mobile display. Is there a way to call the mobile version of index.html on the hosting server rather than having to point to a ‘m.websitename.com’ address? So, could I include javascript to check for screen size and then serve the mobile version of the site? Thanks for helping a coding neophyte!

    1. Hello Michael,

      Yes, you can use the javascript version of the redirect. You would place the code on the default index page for the regular version and simply have it redirect to the other index page if it fit the mobile criteria. For example if mobile-index.html is the mobile version you would use:

      <script type=”text/javascript”>
      <!–
      if (screen.width <= 800) { window.location = "https://domain.com/mobile-index.html"; } //--> </script>Kindest Regards, Scott M

  63. Hello.

    I am using this code:

    <script type=”text/javascript”>
    var getStr = window.location.search.substr(1);
    var getArray = getStr.split (“&”);
    var get = {};
    for ( var i = 0; i < getArray.length; i++) {
    var tmpArray = getArray[i].split(“=”);
    get[tmpArray[0]] = tmpArray[1];
    }
    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
    if(document.URL !=”https://www.m.website.com/page1″)
    {
    if (get.m != “no”) {
    window.location =”https://www.website.com/page1″;
    }
    }
    }
    </script>

    Am I correct in thinking that this will redirect all mobile traffic to the m. subdomain for that page unless it is followed by ?=no

     

    Thanks

    1. Hello Matthew,

      That looks like it may work however I would suggest using the .htaccess method stated in the guide as you are performing that redirect on the web server level and not on the php level.

      Best Regards,
      TJ Edens

    2. Thanks TJEdens.

      I’m not that familiar in using the .htaccess file. I usually get someone else to do it for me!

      I’ve used this example from this thread:

      —-
      RewriteEngine on
      RewriteBase /
      # Check if this is the noredirect query string
      RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
      # Set a cookie, and skip the next rule
      RewriteRule ^ – [CO=mredir:0:www.website.com]

      # Check if this looks like a mobile device
      # (You could add another [OR] to the second one and add in what you
      # had to check, but I believe most mobile devices should send at
      # least one of these headers)
      RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
      RewriteCond %{HTTP:Profile} !^$ [OR]
      RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
      RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

      # Check if we’re not already on the mobile site
      RewriteCond %{HTTP_HOST} !^m\.
      # Can not read and write cookie in same request, must duplicate condition
      RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)

      # Check to make sure we haven’t set the cookie before
      RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]

      # Now redirect to the mobile site
      RewriteRule ^ https://m.website.com [R,L]

      —–

      But I do not want to redirect tablets, only mobile phones and also how do you deal with going back to the desktop site if you wanted to?

      Thanks

    3. I previously had this in my .htaccess but couldn’t get it to work…

      —-
      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 ^ https://m.website.com%{REQUEST_URI} [R,L]
      ——-

      Any suggestions greatly appreciated.

      Thanks

    4. Hello mwrcrentals,
      I am not certain what it is you are trying to do exactly. Are you trying to redirect to mobile based on the device’s ability to accept the mime types listed? It is far more common to redirect based on either screen size or user agent. In any case, have you gotten a single instance to work and then added the others and tested them? That is how you want to build your rewrite rules.

      Kindest Regards,
      Scott M

    5. I want to re-direct mobile phones, not tablets from website.com/page1 to m.website.com/page1 with the option to allow the user to return to the website.com/page1 should they choose. I thought the javascript I posted above …

      —-

      I am using this code:

      Am I correct in thinking that this will redirect all mobile traffic to the m. subdomain for that page unless it is followed by ?=no

      ----

      Would work but TJ Edens suggested that I should be doing all this in my .htaccess file.

      Thanks

  64. This code worked for me, replace the ?????????? with your domain and the ??? with your px values (ex. :  if ( screen.width <= 960) { )

    For ANDROID:

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    <script type=”text/javascript”>

      <!–

      if (screen.width <= ???) {

        window.location = “https://m.??????????.com”;

      }

      //–>

    </script>

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    For I PHONE:

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    <script type=”text/javascript”>

    <!–

       function getParameterByName(name) {

        name = name.replace(/[\[]/, “\\\[“).replace(/[\]]/, “\\\]”);

        var regex = new RegExp(“[\\?&]” + name + “=([^&#]*)”),

            results = regex.exec(location.search);

        return results == null ? “” : decodeURIComponent(results[1].replace(/\+/g, ” “));

    }

    if (screen.width <= ??? && getParameterByName(‘frommobile’)!=”gotomain”) {

    document.location = “https://??????????.com/m/index.html”;

    }

    if (((navigator.userAgent.match(/iPhone/i)) ||  (navigator.userAgent.match(/iPod/i))) &&  (getParameterByName(‘frommobile’)!=”gotomain”)) {

       location.replace(“https://??????????.com/m/index.html”);

    }

    //–>

    </script>

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    Place it in your website header section of your main website, and it should work fine.

    Remember to create your CPanel subdomain, named:

    m.??????????.com

    and when and once you place that code in your header it should look like this:

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    <script type=”text/javascript”>

      <!–

      if (screen.width <= ???) {

        window.location = “https://m.??????????.com”;

      }

      //–>

    </script>

    <script type=”text/javascript”>

    <!–

       function getParameterByName(name) {

        name = name.replace(/[\[]/, “\\\[“).replace(/[\]]/, “\\\]”);

        var regex = new RegExp(“[\\?&]” + name + “=([^&#]*)”),

            results = regex.exec(location.search);

        return results == null ? “” : decodeURIComponent(results[1].replace(/\+/g, ” “));

    }

    if (screen.width <= ??? && getParameterByName(‘frommobile’)!=”gotomain”) {

    document.location = “https://??????????.com/m/index.html”;

    }

    if (((navigator.userAgent.match(/iPhone/i)) ||  (navigator.userAgent.match(/iPod/i))) &&  (getParameterByName(‘frommobile’)!=”gotomain”)) {

       location.replace(“https://??????????.com/m/index.html”);

    }

    //–>

    </script>

    – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

    This took me sometime to figure it out, but finally could do it, so hope I can help you to save sometime, lol.

    Best regards

    JMA274

     

  65. This is very odd. I have tried different variations of all of the userAgent methods here…the only one I could actually get to work was the very simple

    <script>

     

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){

     

    if(document.URL !=”https://mailabrick.com”)

     

      {

     

        window.location =”https://mailabrick.com”;

     

      }

     

    }

     

     

    </script>

    The problem with that is that it then just puts the mobile user in a redirect loop, never able to access the main site if they wish. I’m a bit confused. Thanks for any help that anyone might be able to offer.

    1. Hello Josh,

      Your script would work the first time but once it redirects the script will see that the agent is still mobile and redirect again. I would suggest putting an if else statement above the user agent check. So if url eqauls mobile url dont redirect, else redirect if user agent equals any of these.

      Kindest Regards,
      TJ Edens

  66. <script type=”text/javascript”>

     

    <!–

     

    var getStr = window.location.search.substr(1);

     

    var getArray = getStr.split (“&”);

     

    var get = {};

     

     

     

     

     

    for ( var i = 0; i < getArray.length; i++) {

     

    var tmpArray = getArray[i].split(“=”);

     

    get[tmpArray[0]] = tmpArray[1];

     

    }

     

     

     

    if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

     

    if(document.URL !=”https://www.m.nrgelectric.ca”)

     

    {

    if (get.m != “no”) {

    window.location =”https://www.m.nrgelectric.ca”;

    }

    }

     

    }

    //–>

     

    </script>

    okay okay it works great, is there anyone who could help me out with cookies?

    I want the script too..Redirect Once and the Cookie to Expire in: days.

  67. Hi Scott

     

    I finally got it working with the following coding

     

    <script type=”text/javascript”>

     

    <!–

     

    var getStr = window.location.search.substr(1);

     

    var getArray = getStr.split (“&”);

     

    var get = {};

     

     

     

     

     

    for ( var i = 0; i < getArray.length; i++) {

     

    var tmpArray = getArray[i].split(“=”);

     

    get[tmpArray[0]] = tmpArray[1];

     

    }

     

     

     

    if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

     

    if(document.URL !=”https://www.irhwebsites.com/mobile”)

     

    {

    if (get.m != “no”) {

    window.location =”https://www.irhwebsites.com/mobile”;

    }

    }

     

    }

    //–>

     

    </script>

  68. Hi Scott

     

    Thank you for the reply, appreciate it

    I have got a iphone, I will see if the user agent code is better for me, thank you appreciate it

     

    Ian

  69. Hi

     

    I am using the following coding that I got from above but when on my mobile it defaults to the main site and not the mobile version, have I done the url’s wrong

     

    <script type=”text/javascript”>

     

    <!–

    if (get.m != “no”) {

     

    window.location =”https://www.irhwebsites.com/mobile/”;

     

    }

    else if (screen.width <= 800) {

     

    window.location = “https://www.irhwebsites.com/mobile/”;

     

    }

    //–>

     

    </script>

    1. Hello Ian,

      What type of phone are you using? If you have a phone that has a display larger than 800, then it will display as the main site. You would need to use the code that checks for the user agent instead.

      Kindest Regards,
      Scott M

  70. im using the bellow code, howeveer it doesnt seam to work well because its moving it to m.mysite.com and i want it to be mysite.com/mobile.php so when it does the mobile check it times out of to many forwards. and on a side note, when i put in the ?m=no it works fine, but if i click a link within the desktop site it messes up…

     

    # Check if this is the m=no query string
    RewriteCond %{QUERY_STRING} (^|&)m=no(&|$)
    # Set a cookie, and skip the next rule
    RewriteRule ^ – [CO=mredir:0:www.example.com]
    # Check if this looks like a mobile device
    RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
    RewriteCond %{HTTP:Profile} !^$ [OR]
    RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
    RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

    # Check if we’re not already on the mobile site
    RewriteCond %{HTTP_HOST} !^m\.
    # Can not read and write cookie in same request, must duplicate condition
    RewriteCond %{QUERY_STRING} !(^|&)m=no(&|$) 

    # Check to make sure we haven’t set the cookie before
    RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
    # Now redirect to the mobile site

    RewriteRule ^ https://m.example.com [R,L] 

    1. Hello vishal,

      Thank you for contacting us. The javascript is provided in the section above labeled Javascript window.location Method.

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

  71. I agree, responsive design is does well for SEO but if you follow Googles rule with canonical links tags you can avoid having duplicate content and it will rank just as good.

    https://moz.com/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps

  72. Scott that makes sense now.. I set it both to our mobile site and then set the link back code to m=no now instead.  But when I go to our main site it doesn’t even forward to the mobile site which it should since I am on an HTC One.

    1. Hello Tim,

      That is very possible as my phone does the same (Galaxy Note 4). Both are 1080p resolution phones, so they do not fit the qualifications based on the screen width. To ensure ALL mobile devices switch to the mobile site, they would need to be based on user agent instead of screen width. Since the 1080p phones are essentially hand held monitors, they typically display a full site fine, so many people go with screen width of less than 800.

      Kindest Regards,
      Scott M

    2. It looks like you’re writing the code a bit backwards, as well as missing a semicolon. Your code should look like the following:

      <script type=”text/javascript”><!–
      var getStr = window.location.search.substr(1);
      var getArray = getStr.split (“&”);
      var get = {};
      for ( var i = 0; i < getArray.length; i++) { var tmpArray = getArray[i].split("="); get[tmpArray[0]] = tmpArray[1]; }if (get.m === "no"){ window.location = "https://mainsite"; } else if (screen.width <= 800) { window.location = "https://mobilesite"; } //--> </script>

  73. I am so close to getting this I must just have one thing missing.. found this code below that works great for the mobile forward then when i click the link on my mobile to go back to the main site it works for a second but then re does the redirect based off screen size.. here is my code:

    <script type=”text/javascript”>        

    var getStr = window.location.search.substr(1);    

        var getArray = getStr.split (“&”);  

          var get = {};        for ( var i = 0; i < getArray.length; i++) {        var tmpArray = getArray[i].split(“=”);        get[tmpArray[0]] = tmpArray[1];        }       if (get.m == “yes”) {                

     window.location = “https://wwww.mainsite.com/content”        }    

         else if (screen.width <= 800) {                

     window.location = “https://www.mobilesite.com”;     

        } //–>  //–></script>

     

    Then my code on the mobile site is this:

    https://www.mainsite.com/content/?m=yes

    1. Hello Tim,

      The code was intentionally written to have both go to mobile site. The first condition says If NOT not mobile (a double negative meaning if it is set to mobile) then go to mobile. The second check looks for screen resolution. If it is less than 800, then go to mobile. Please check these conditions on Jeff’s original code. Neither of these should point to the main site. The main site is the default, so if both of these conditions fail, then the main site should appear. Granted this could have been written with better logic, but that is how it should work.

      Kindest Regards,
      Scott M

  74. Below is what I have now.. neither part works for some reason?? It doesn’t forward me to the mobile site even.. 

    <script type=”text/javascript”>

     

    <!–

    if (get.m != “no”) {

     

    window.location =”https://www.mainsite.com”;

     

    }

    else if (screen.width <= 800) {

     

    window.location = “https://www.mobilesite.com”;

     

    }

    //–>

     

    </script>

  75. Scott why would both of the links be to the mobile site??? If I have a link back from my mobile site and the code you have there is on my main site wouldn’t it keep sending me always back the mobile site??

    1. Hello Tim,

      I’m sure Scott had actually meant to copy in paste in the other site. It was a typo on our part so our apologies. The condition of the first statement should lead to the mobile site , but if it doesn’t, it should get to the next statement and re-direct to your other site. That logic didn’t seem to be set in the code that was previously there.

      Apologies again for the confusion. I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

  76. Okay I updated this is what I have now

    <script type=”text/javascript”>

      <!–

      if (screen.width <= 800) {

        window.location = “https://www.mobile.com”;

      }

     

    if (get.m = “no”) {

    window.location =”https://www.main.com”;

    }

      //–>

    </script>

     

    And my link on my mobile to go back to my full page is “https://www.mainsite.com?m=no” but everytime I use that link it still re directs me to the mobile instead of reading the “No” and sending me to the main site

    1. Hello Tim,

      The code that Jeff provided is working for me. What is the screen resolution you are using? If that one triggers, then the second conditional should not fire. You may want to try a variant of this code to see if it helps:

      <script type=”text/javascript”>

      <!–
      if (get.m != “no”) {

      window.location =”https://www.mobile.com”;

      }
      else if (screen.width <= 800) {window.location = "https://www.mobile.com";} //--></script>

      Kindest Regards,
      Scott M

    2. Hello Tim,

      The last part of the code reads a bit odd, it should not be:

      if (get.m != “no”) {window.location =”https://www.mainsite.com”;}

      But rather:

      if (get.m != “no”) {window.location =”https://www.mobile.com”;}

      It basically reads “If get.m is NOT ‘no’ then go to the mobile site.”

      Kindest Regards,
      Scott M

  77. Any idea why this code isn’t working?? The mobile re direct works but when I come back to the site from a link with m=no? it still keeps re directing to my mobile site??

     

    <script type="text/javascript">
      <!--
      if (screen.width <= 800) {
        window.location = "https://www.mobilesite.com";
      }
    
    if (get.m != "no") {
    window.location ="https://www.fullsite.com";
    }
      //-->
    </script>
    1. Hello Tim,

      Your re-direct to the full site is using a double-negative (“not no”), then re-direct. It should be set to = “no”. Try that change and then let us know if you require any further assistance.

      Regards,
      Arnel C.

  78. My question is how’s come the script that JeffMa put on here doesn’t work for me.. I take it as if it sees andriod, ios, etc it sends to the mobile then if m=no then it should take me to my main site is that not how it should work? I can’t get it to send me to the mobile site from this code

     

     <script type=”text/javascript”>

    <!–

    var getStr = window.location.search.substr(1);
    var getArray = getStr.split (“&”);
    var get = {};
    for ( var i = 0; i < getArray.length; i++) {
    var tmpArray = getArray[i].split(“=”);
    get[tmpArray[0]] = tmpArray[1];
    }
    if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){
    if(document.URL !=”https://www.mobilesite.com”)

    {
    if (get.m != “no”) {
    window.location =”https://www.mainsite.com”;
    }
    }

    }
    //–>

    </script>

  79. Hello there Brad. Great post by the way. It’s really interesting and full of information. I happen to be interested in the findings of using PHP programming language to redirect user to another page and I found this one https://www.lionleaf.com/blog/how-to-redirect-using-php/ which gives me positive output as well but your ideas are realistic and pretty much helpful to my research as well. Can you please explain how redirect reflects Google search result?

    1. Hello Jars Jones,

      Thanks for the question, and it’s a good one. The option to re-direct to a mobile version of a website is actually considered an outdated option. New website design focuses on “responsive design” in order to have all traffic coming from one location – for the very reason of SEO. A properly created responsive website will be able to adjust for whatever device is accessing it. If you do have a mobile site, you will not want to have any ranking for that site, or you would need to select that site OVER your other site for the website that you wish to be indexed. Having multiple sites with ranking goes into the whole issue of duplicate content which can lead to problems for SEO. Ideally, you’d want all of the ranking for SEO to be on one web site. So, in terms of the affect on the Google search result – it should have very minimal effect unless you intend for the mobile site to be the site you intend to have indexed for searches.

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

  80. I used this code below but friends of mine that have Samsung Galaxy S$ phones told me the site is not redirecting any idea what i need to add to this code? ..i also was wondering can I add Chrome to this and what would it be called or look like. 

    <script>

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

    if(document.URL !=”https://mobile.dudamobile.com/site/pringroofing3″)

    {

    window.location =”https://mobile.dudamobile.com/site/pringroofing3″;

    }

    }

    </script>

    1. Hello Kevin,

      All the settings I have seen have Android in a non-capitalized word: android. This may be the cause. I have seen others also add ‘galaxy’ as well. Please try these and see if it helps.

      Kindest Regards,
      Scott M

  81. <script type=”text/javascript”>

     

    <!–

     

    var getStr = window.location.search.substr(1);

     

    var getArray = getStr.split (“&”);

     

    var get = {};

     

     

     

     

     

    for ( var i = 0; i < getArray.length; i++) {

     

    var tmpArray = getArray[i].split(“=”);

     

    get[tmpArray[0]] = tmpArray[1];

     

    }

     

     

     

    if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

     

    if(document.URL !=”https://www.m.nrgelectric.ca”)

     

    {

    if (get.m != “no”) {

    window.location =”https://www.m.nrgelectric.ca”;

    }

    }

     

    }

    //–>

     

    </script>

    THIS WORKED AWESOME, thank you!! (This is a good code to have the “view full site,” link with).

    /?=no             on the link you dont want to render mobile

  82. I am not really familiar with PHP.  I copied the code belkow from the responses above and whe I use the the php or the java code by itrself there are no errors but when I put the java code inside the php where it says it “JAVASCRIPT HERE” i get a syntax error on line 3 see below:

     

    <?php if ($_GET[‘mobile’] !== 0) {

     

    echo “<script type=”text/javascript”>  <!–  if (screen.width <= 800) {    var xx = confirm(“You are using a mobile device.\nPress Cancel to view the Full Desktop site.”);      if (xx == true) {    x = window.location = “https://m.singleglow.com”;} else {    x = window.location = “https://singleglow.com”;}  }  //–></script>”;

     

    } ?>

     

    Can anyone give me an idea as to what I am doing wrong?

    1. Hello Frank,

      Just one thing and then you can check to see if the code is correct. You will want to remove the ‘echo’ and the surrounding quotes. Echo just prints whatever is in it to the screen, so it will not actually run.

      Give that a try and see if it works for you.

      Kindest Regards,
      Scott M

  83. Hi, my question is: “is it efficient to include a correspponding mobile-link to each page on my site. In case a user doesnt land on my index page as assumed in most examples av seen online.”

    <form action = “page1.php”>

    <script type=”text/javascript”>

    <!–

    if (screen.width >= 320 || screen.width <= 480 ) {

    document.location = “m.mydomain.com/page1.php”;

    }

    //–>

    </script>

    </form>

     

    <form action = “page2.php”>

    <script type=”text/javascript”>

    <!–

    if (screen.width >= 320 || screen.width <= 480 ) {

    document.location = “m.mydomain.com/page2.php”;

    }

    //–>

    </script>

    </form>

    etc… ??

    1. Hello John,

      While the best method would be to have a responsive site, so that you have one site that changes depending on the connecting device, adding that small amount of code to each page will not add any significant delay to your site loading time. It is small enough to be efficient and should work nicely.

      Kindest Regards,
      Scott M

  84. I noticed an LG Chocolate didn’t redirect, and the stats say Polaris.

     

    Is this “another” mobile browser to be put into…

     

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){

     

    Thanks in advance

  85. Hello Team

    I have used the code for mobile redirects mentioned above. That works perfectly. Thanks.

    Now I have a different situation. All the pages when accessed from mobile go to mobile home page. I need these to work relatively, such as a page home.com/inner1.php should be redirected to m.home.com/inner1.php.

    Another query is there are some pages which are not mobile ready, which is also set. Query is about stopping redirection from specific folders and all the pages in that folder. The files and pages should not be redirected, i mean they should be restricted from redirection. The code i use is as belows.

     

    <Files .htaccess>

    order deny,allow

    deny from all

    </Files>

     

    Options +FollowSymLinks -Indexes

     

    php_flag display_startup_errors on

    php_flag display_errors on

    php_value error_reporting 2047

    php_value magic_quotes_gpc off

     

     

    RewriteEngine On

    RewriteBase /

    RewriteCond %{HTTP_HOST} !^www\.

    RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [R=301,L]

     

    # Check if this is the noredirect query string

    RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)

    # Set a cookie, and skip the next rule

    RewriteRule ^ – [CO=mredir:0:www.home.com]

     

    #urls to exclude

    RewriteCond %{REQUEST_URI} !^/subfolder/innerpage1.php

    RewriteCond %{REQUEST_URI} !^/subfolder/images/image1.png$

     

    RewriteCond %{HTTP:x-wap-profile} !^$ [OR]

    RewriteCond %{HTTP:Profile}       !^$ [OR]

    RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT}  “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]

    RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]

    RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

     

    # Check if we’re not already on the mobile site

    RewriteCond %{HTTP_HOST}          !^m\.

    # Can not read and write cookie in same request, must duplicate condition

    RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) 

     

    # Check to make sure we haven’t set the cookie before

    RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

     

    # Now redirect to the mobile site

    RewriteRule ^ https://m.home.com [R,L]

     

    Please help me with the code for new redirects.

    Thanks

    Somesh.

    1. Hello Somesh,

      Thank you for your questions. The format for redirecting relatively should follow a similar format as the 301 redirect for relative redirection, of course your conditional code to detect mobile browsers will still apply.

      Im not sure about stopping redirection, except that the htaccess will read top to bottom. You may want to put individual rules for the specific pages. I am not sure how that would be accomplished off hand without specific testing.

      Remember that redirection to mobile versions was a trick before responsive design was popular. That is the best way to go if you are still designing a site. This prevents you from having two separate websites to maintain.

      Kindest Regards,
      Scott M

  86. Hi,

     

    Im fearly new to web design, i have build a website with html/css and want to re-direct users from desktop to mobile. help!

    1. Hello Darwin,

      Thank you for contacting us. Have you tried any of the suggestions above?

      Also many Content Management Systems (CMS) such as WordPress, Joomla, or Concrete5 have Addons/Templates that can make the site responsive. Meaning they respond to the size of the viewer’s screen, instead of redirecting to a separate mobile website.

      Thank you,
      John-Paul

  87. I have a website – www.blownawayconference.ca – that works fine on desktop but when i type in the address on my phone it defaults to mobile.blownawayconference.ca – I am using a wordpress site and have zero plug ins activitated that would redirect.  I am with godaddy and I deleted this from the .htaccess file because it looked like it was redirecting to this mobile address – but still no changes on my phone – any ideas?  should i change something in the coding below that I deleted:

    ##BEGIN MOBILE (do not edit below this line)
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} ((.*iPhone.*)|(.*iPod.*)|(.*BlackBerry.*)|(.*Android.*Mobile.*)|(.*Windows\ CE.*)|(.*IEMobile.*)|(.*Opera\ Mini.*)|(.*Opera\ Mobi.*))
    RewriteCond %{HTTP_COOKIE} !dm_show_classic
    RewriteCond %{QUERY_STRING} !no_redirect=true [NC]
    RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
    RewriteCond %{HTTP_HOST} ^[0-9a-z-]+\.[a-z]+$ [NC]
    RewriteCond %{REQUEST_URI} ^/_dm/s/ [NC,OR]
    RewriteCond %{REQUEST_FILENAME} !\.(jpg|gif|png|css|js|txt|ico|pdf|bmp|tif|mp3|wav|wma|asf|mp4|flv|mpg|avi|csv|doc|docx|xls|xlsx|ppt|pptx|zip|rar|tar|gz|dmg|iso)$ [NC]
    RewriteBase /
    RewriteRule ^(.*)$ https://mobile.blownawayconference.ca/ [R,L]
    ##END MOBILE

     

    1. Hello Cindy,

      The code you have deleted is definitely code that redirects to a mobile domain. As to why your site still redirects after the removal of the code, it could be a few things. Additional code could be in place that redirects, this can be done via javascript for example. However, if you are certain you have removed all the code from your application that does the redirect, the most likely cause is caching. You may want to clear the cache of the browser to see if that helps. If not, over time, the cached page will clear on its own and then it should behave properly.

      Kindest Regards,
      Scott M

  88. I also tried this code:

    <script type=”text/javascript”>
     
      if (screen.width <= 800) {
        window.location = “https://www.m.lescabanesdepyrene.com”;
      }
      //
    </script>

    but same problem..

    I developed the site with dreamweaver, HTML and PHP. it is only a single page in the expectation of a future responsive website.

    1. It seems like something within your code is causing the redirect. Unfortunately, I do not have any advice as to exactly what is there that is causing it, but I recommend looking through each line of code for any potential issues.

    2. Great article! I’m a web developer. The htaccess solution is of interest to me. The best way to improve would be more code samples with well-written annotations (though that’s a lot to ask).

  89. Hello, thanks for sharing!

    Sorry for my English as I didn’t practise for a while.

    I used the Javascript window.location Method and placed it in the hed section but it didn’t work.. I have a loop redirect. You can check it at lescabanesdepyrene.com

    the code below:

    <script>
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    window.location.href=”https://www.m.lescabanesdepyrene.com/?m=1″;
    }
    </script>

    </head>

     

    Could you please help me to find what am I doing wrong?

    Thanks!!

    1. It appears that there is additional code on the mobile site that is causing the redirect. How was the mobile site developed?

  90. I duplicated a website and modified from there. I’d like to change the name of the duplicate’s mobile version to it’s new name, how do I go about that?

    Help please 🙂

    1. Hello Anna,

      Thank you for your question. We are happy to help, but will need some additional information.

      What type of CMS are you using? For example are you using WordPress, Joomla, Concrete5, etc.?

      This will help us determine the best method for updating your URL.

      Thank you,
      John-Paul

  91. make this at index.html

    —-
    <script type=”text/javascript”>
      <!–
      if (screen.width <= 800) {
        var xx = confirm(“You are using a mobile device.\nPress Cancel to view the Full Desktop site.”);
          if (xx == true) {
        x = window.location = “https://clwalker.ddns.net/mobile/index.html”;
    } else {
        x = window.location = “https://clwalker.ddns.net/index2.html”;
    }
      }
      //–>
    </script>
    —-
    popup will cancel to full desktop site …. ok will continue to mobile

    worked for me 🙂
    thanks to all of you for the help!

    1. I have query regarding how to find the location of the user who is accessing my website from anywhere in the world(using Javascript or/And MVC C#)

    1. Hello delysid,

      You can put a delay counter in the code if you are using php or javascript for the redirect, but not for the htaccess.

      Kindest Regards,
      Scott M

  92. Is there someone that can help me find someone to take over my website that is written in code?  I really need some help. The company I hired won’t respond to me anymore since I made the final payment to them, and now there are things not working on it, and the mobile version is a nightmare, and I’m losing tons of sales because of this.  I don’t understand how to work in code, so I am hoping someone out there might be able to.   My domain is www.amandasmithphotography.com and my email is [email protected]

    1. Hello Amanda,

      Thank you for contacting us. Our Web Design team can help you build a new site, but unfortunately will not take over an existing one. You may want to call them and discuss possible options for your website.

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

  93. Thanks John-Paul

    .htaccess works great!

    Only thing is it seams to be having a problem with is Kindle Fire.

    Can something be added to make it work with a Kindle?

    Thanks

    1. Hello Chris,

      You would just add silk and kindle to the user agent strings such as:

      RewriteCond %{HTTP_USER_AGENT} “silk|kindle” [NC,OR]

      Kindest Regards,
      TJ Edens

  94. hello can u tell me how i can redirect my website to mobile user my site is androidtrade.com plz help me.

  95. Hello John-Paul,

    Not working yet but a few questions;

    1. Does it matter where in the .htaccess file I place the code you gave me?

    2. The last line you gave me is: RewriteRule ^ https://m.example.com [R,L]

    should I change that url to the url of my mobilesite which would be:

    https://www.1777americanainn.com/mobilesite

    3. I removed the apostrophies in the words “we’re” and “haven’t” – does that sound right?

     

    1. Hello Dave,

      Thank you for contacting us again. 1. It is recommended to put the code on the top of your .htaccess file. It may also be helpful to create a new .htaccess file. This would allow you to rule out any other existing rules interfering with what you are testing.

      2. Yes that is correct, replace the example with your actual mobile site.

      3. It does not matter. Any line in the .htaccess file beginning with “#” is just a comment, and does not affect the code.

      I hope this helps,
      John-Paul

  96. Hello John-Paul,

    Thanks for that. Should I also be removing this block of javascript that I was trying to use to solve the problem?:

    <script type=”text/javascript”>

    <!–

    var getStr = window.location.search.substr(1);

    var getArray = getStr.split (“&”);

    var get = {};

    for ( var i = 0; i < getArray.length; i++) {

    var tmpArray = getArray[i].split(“=”);

    get[tmpArray[0]] = tmpArray[1];

    }

    if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

    if (get.m != “no”) {

    window.location =”https://www.1777americanainn.com/mobilesite”;

    }

    }

    //–>

    </script>

     

     

     

    1. Hello Dave,

      Yes, remove the initial javascript added previously.

      This alternate solution uses the .htaccess file instead.

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

  97. Hi Arnel,

    Here it is:

     # -FrontPage-

     

    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

     

    <Limit GET POST>

    order deny,allow

    deny from all

    allow from all

    </Limit>

    <Limit PUT DELETE>

    order deny,allow

    deny from all

    </Limit>

    AuthName 1777americanainn.com

    AuthUserFile /home/amer1x/public_html/_vti_pvt/service.pwd

    AuthGroupFile /home/amer1x/public_html/_vti_pvt/service.grp

     

     

     

    RewriteEngine ON Options -Indexes

    DirectoryIndex index.html

    DirectoryIndex index.php

     

    RewriteBase /

    RewriteCond %{HTTP_HOST} ^1777americanainn.com [NC]

    RewriteRule ^(.*)$ https://www.1777americanainn.com/$1 [R=301,L,NC]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 

    RewriteRule ^index\.html$ https://www.1777americanainn.com [R=301,L,NC]

     

    RewriteRule ^rooms.html https://www.1777americanainn.com/ephrata-bed-and-breakfast.html [R=301,L,NC]

     

    # BEGIN EXPIRES

    <IfModule mod_expires.c>

    # Enable expirations

    ExpiresActive On 

    # Default directive

    ExpiresDefault “access plus 1 month”

    # My favicon

    ExpiresByType image/x-icon “access plus 1 year”

    # Images

    ExpiresByType image/gif “access plus 1 month”

    ExpiresByType image/png “access plus 1 month”

    ExpiresByType image/jpg “access plus 1 month”

    ExpiresByType image/jpeg “access plus 1 month”

    # CSS

    ExpiresByType text/css “access 1 month”

    # Javascript

    ExpiresByType application/javascript “access plus 1 month”

    </IfModule>

    # END EXPIRES

     

     

    <ifModule mod_gzip.c>

    mod_gzip_on Yes

    mod_gzip_dechunk Yes

    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$

    mod_gzip_item_include handler ^cgi-script$

    mod_gzip_item_include mime ^text/.*

    mod_gzip_item_include mime ^application/x-javascript.*

    mod_gzip_item_exclude mime ^image/.*

    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

    </ifModule>

     

     

    1. Hello Dave,

      Thank you for your question. There are several ways to accomplish this, below is the best solution we could find.

      Add the following lines to your .htaccess file (be sure to replace example.com with your actual domain name):

      # Check if this is the m=no query string
      RewriteCond %{QUERY_STRING} (^|&)m=no(&|$)
      # Set a cookie, and skip the next rule
      RewriteRule ^ – [CO=mredir:0:www.example.com]
      # Check if this looks like a mobile device
      RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
      RewriteCond %{HTTP:Profile} !^$ [OR]
      RewriteCond %{HTTP_USER_AGENT} “acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “wapp|wapr|webc|winw|winw|xda|xda-” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “up.browser|up.link|windowssce|iemobile|mini|mmp” [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} “symbian|midp|wap|phone|pocket|mobile|pda|psp” [NC]
      RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]

      # Check if we’re not already on the mobile site
      RewriteCond %{HTTP_HOST} !^m\.
      # Can not read and write cookie in same request, must duplicate condition
      RewriteCond %{QUERY_STRING} !(^|&)m=no(&|$)

      # Check to make sure we haven’t set the cookie before
      RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
      # Now redirect to the mobile site
      RewriteRule ^ https://m.example.com [R,L]

      (Thanks to StackOverflow for this solution.)

      Then, on your mobile site, link back to the Full Site with the following variable on the end:
      ?m=no

      For example:
      https://www.1777americanainn.com/mobilesite/index.html?m=no

      Note: If the user has cookies disabled, they will see the mobile site by default.

      I also noticed a RewriteRule, with a missing RewriteCond here:

      RewriteRule ^rooms.html https://www.1777americanainn.com/ephrata-bed-and-breakfast.html [R=301,L,NC]

      If you have any further questions, feel free to post them below.

      Thank you,
      John-Paul

    1. Hello Dave,

      Apologies for the confusion. I did not see the file anywhere. Where did you send it? If you paste in the reply here I’ll see it immediately.

      Regards,
      Arnel C

  98. This is the top part of the file (at some point this was edited by some seo company who were working on the site):

    # -FrontPage-

    IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

    <Limit GET POST>

    order deny,allow

    deny from all

    allow from all

    </Limit>

    <Limit PUT DELETE>

    order deny,allow

    deny from all

    </Limit>

    AuthName 1777americanainn.com

    AuthUserFile /home/amer1x/public_html/_vti_pvt/service.pwd

    AuthGroupFile /home/amer1x/public_html/_vti_pvt/service.grp

     

     

     

    RewriteEngine ON Options -Indexes

    DirectoryIndex index.html

    DirectoryIndex index.php

    1. Hello Dave,

      Much of what you copied to us are typical Frontpage entries. Can you please provide the entire file?

      Kindest regards,
      Arnel C.

    1. Hi Dave,

      It’s possible that there is something in .htaccess file causing this conflict. Can you provide a copy of the .htaccess file so that we can see why the code is still not working?

      Thanks!
      Arnel C.

  99. Hello Arnel,

    Thanks but that’s still not working.

    Are you able to link back to the full site from the mobile site?

    Any chance that something in my .htaccess file could be preventing this from working?

     

     

    1. Hello Dave,

      Thanks for the patience. It appears that Jeff had wanted the following removed:

      Delete this from script

      You should then have only the following left:

      Delete this from script

      Please make these changes and then check the site once more.

      Regards,
      Arnel C.

  100. Hello Arnel, Thanks for checking.

    Yes, I’m constantly clearing my browser caches on all devices. And I’m still having the problem on all my devices – desktop, ipad & iphone. The link on the mobile site that I want to lead back to the “Full Site” just keeps pulling up the mobile site. 

    Are you saying that it works for you?  

    https://www.1777americanainn.com/mobilesite/

    Thanks for your help!

    1. Hello Dave,

      Jeff and I looked at your site again. It looks like that code in the javascript was interfering with the link. Remove the following from the script that Jeff gave to you:

      {
      if(document.URL !=”https://www.1777americanainn.com/mobilesite”)
      }

      Make sure that you’re removing one of the closing brackets under that statement and the opening bracket that starts the “if” line. That should allow the site to return to your full site.

      Regards,
      Arnel C.

    1. Hello Dave,

      Sorry you’re still having problems with the re-direct. I need to check and make sure that you cleared your browser cache. If it’s working for us, but not you, then we need to double-check to make sure that issues such as caching are not causing the problem.

      Kindest regards,
      Arnel C.

  101. I wonder if there’s something in my .htaccess file that’s preventing this script from working. I know that some seo company altered that file at some point in the past.

    1. When I look at your home page, it looks like you do not have this new redirect in there. Have you changed it?

  102. I deleted the 2 other scripts and pasted the above in the head section of the main site but it’s still a no-go. The main site link on the mobile site still keeps pulling up the mobile site.  

    1. I have fully tested the code and confirmed that it is indeed redirecting my phone when normally but when adding ?=no to the end of the URL, the main site is visited. Keep in mind that this new code will need to replace any other redirects directly on the main site and all other pages if needed.

  103. Yeah, but if I take away the first script then my smartphone is no longer automatically directed to the mobile site which was the main thing I wanted to accomplish. 

    Maybe I’ll try doing the redirect to the mobile site via the .htaccess rewrite as suggested at the top of the page?

     Thanks again for your help.

    1. This is a modified version of the script that should work for you:

      <script type=”text/javascript”>

      <!–

      var getStr = window.location.search.substr(1);

      var getArray = getStr.split (“&”);

      var get = {};

      for ( var i = 0; i < getArray.length; i++) {var tmpArray = getArray[i].split("=");get[tmpArray[0]] = tmpArray[1];}if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){if(document.URL !="https://www.1777americanainn.com/mobilesite"){ if (get.m != "no") { window.location ="https://www.1777americanainn.com/mobilesite"; } }} //--></script>

  104. Sorry, I meant to say my redirect link on the mobile site is: https://www.1777americanainn.com?m=no but that still isn’t redirecting for me. 

    On the main site I’ve added the following two scripts after reading this thread:

    <script>

    if(/Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent)){

    if(document.URL !=”https://www.1777americanainn.com/mobilesite”)

    {

    window.location =”https://www.1777americanainn.com/mobilesite”;

    }

    }

    </script>

     

    <script type=”text/javascript”>

    <!–

            var getStr = window.location.search.substr(1);

            var getArray = getStr.split (“&”);

            var get = {};

     

     

            for ( var i = 0; i < getArray.length; i++) {

            var tmpArray = getArray[i].split(“=”);

            get[tmpArray[0]] = tmpArray[1];

            }

     

     

           if (get.m == “yes”) {

                    window.location = “https://www.1777americanainn.com/mobilesite”

            }

            else if (get.m == “no”) {

                    window.location = “https://www.1777americanainn.com”;

            }

     

     

     //–>

    </script>

    On the mobile site I have the lnk:

    <li><a href=”https://www.1777americanainn.com?m=no” target=”_blank”>Full Site</a></li> 

    Do you see anything missing? That link continues to pull up the mobile site on the iphone. Really appreciate your help!

     

       

     

     

     

  105.  

     That doesn’t seem to work for me: 

    https://www.1777americanainn.com/mobilesite/?m=no

    On my iphone that url still won’t take me back to the non-mobile site (I’ve tried it with & without the / before the ?)

     

     

    1. The code needs to run on your main site and needs to redirect to the main site. The link would be the following:
      https://www.1777americanainn.com?m=no

  106. I’m trying to use the Javascript window.location method and pasted the following in the head section of the index page of my website:

    <script type=”text/javascript”>

      <!–

      if (screen.width <= 800) {

        window.location = “https://m.domain.com”;

      }

      //–>

    </script>

    That seems to work well. Now I would like to have an option to return to the main site however my mobile site is in a subfolder of the main site, not a subdomain: https://domain.com/mobilesite and my site is a custom coded site, not a CMS.

    I tried using the JavaScript offered by Jacob at the top of this thread but that didn’t work. Anyone know an easy way to do this? 

    – Thanks!

     

     

    1. The solution provided by Jacob does indeed work, although your links to redirect back to the non-mobile site would need to be formatted similar to the following:

      https://example.com?m=no

  107. Where do I place these snippits of code to redirect the phones?

    <script>
    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/IEMobile/i.test(navigator.userAgent)){
    if(document.URL !=”https://m.example.com/products”)
    {
    window.location =”https://m.example.com/products”;
    }
    }
    </script>

    1. Hello Micky,

      Since you’re running a script to detect how the page should redirect a visiting device, it would need to go on the page that your viewer first gets to. Though, to be honest, modern websites that are being developed today no longer use redirection based on device type. Websites are being made with responsive design so that the site automatically adjusts regardless of the viewing device. You can learn more about responsive design by going to Introduction to creating a responsive web design.

      Regards,
      Arnel C.

  108. This is a great article. Thank you for it. I have a different issue I was hoping you could help with: I’m trying to REMOVE the redirect. My site is pretty responsive and the mobile site is really ugly. SO how do I remove the redirect? Thank you for your help 🙂

    1. Take a look through your .htaccess file and look for any redirects that may be listed. If there are not any there, your code itself may be controlling the redirect which would then be completely subjective to how your website is developed.

  109. Hello

     

    I have a wp theme that has a page template specially for mobile users so i want to redirect the users to that mobile page on same domain & from same wp theme but a page specially done for mobile devices having limited content. So when a user will access the site say : https://www.example.com they should be redirected to https://www.example.com/mobile now this page also has a link for mobile users to see the full site version thats https://www.example.com so when the user clicks this link they should be able to see the full site version too irrespective of the fact that its accessed from a mobile device.

     

    Can you pl help me get this working ??

     

    Regards

    Renu

    1. Hello Renu,

      You may want to look into a more up to date responsive theme. Having two separate versions of the site was common at first, but most programs are moving past that in favor of the responsive designs so that their site works on all screen types. Still, many people do redirect to a mobile site based on either the device type or screen size. We cannot assist with any information you have given so far, but you will want to check the redirect code the theme placed in the .htaccess file if it is not working.

      Kindest Regards,
      Scott M

    1. Hello Rajesh,

      You will not be able to access or edit an .htaccess file with blogger. However, they do have a way to redirect their regular blog to a mobile version. You will need to log in and then go to the template area.

      Within the template area you will need to edit the HTML for the web version and place the following code just after the <head> tag and save it.

      <script>
      if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
      window.location.href=”https://yourblog.blogspot.com/?m=1″;
      }
      </script>

      Be sure to change ‘yourblog.blogspot.com’ to your actual blog URL, keep the ‘/?m=1’ as that is part of your mobile version URL.

      Kindest Regards,
      Scott M

  110. Hi, I’ve a bit different question. I’ve a wordpress site. I’m using a plugin (Mobile Domain) to set up a redirect to mobile site. it is actually a sub domian only and it is fatching content from main site. Problem I’m having is when I access my site adding www it is not redirecting. If I access without adding www its redirecting fine. Strange thing is that, I have a sub domain as well but with that I have no such issue. It is working fine eaither with www or without it. Can you please suggest some possible solution for this problem? Thanks!!

    1. I’m not seeing anything within the code of this plugin that would cause it, although some other plugins you have installed may be interacting with it to cause issues. I recommend disabling other plugins to test it as well as fully clearing your browser cache as there could be some cached data in there that is causing the issues that you are experiencing.

      If you’re still not able to get things working after clearing your browser cache as well as disabling other plugins, I recommend reaching out to the plugin developer for more information.

  111. Hows does all this affect big brother Google?

    I was just giving a manual action

    Cloaking and/or sneaky redirects

    Some pages on this site appear to be cloaking (displaying different content to human users than are shown to search engines) or redirecting users to a different page than Google saw.

    My redirects are/were php.  

    All desktops were sent to the desktop version of the site. (not done building the redirects for all counties for desktop.)

    Mobile devices are sent to different mobile areas of my site depending on country of origin.

    If the script can not identify the country, they all go to the default mobile area.

    Since my content is mobile and country specific, I now have no way of sending my visitors to the area of the site they need to see first.

    This is literally costing me money as usa, australia, german, and french visitors now see the same content.  (just an example)

    responsive design is not an answer as my content is different depending on the country and the users device. 

     

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

     

    Thanks

  112. i think you can use this tool for check redirect and you can choise agent mobile device

    https://www.kythuatmang.net/url-redirect-checker.php

  113. Hi Scott,

    I think I have it now.  I tried the Screenfly online mobile emulator and it doesn’t show my mobile test webpage but when I try it in the Browserstack online emulator it seems to working on at least some of the mobile devices they show.

    Can you recommend an good online “free” mobile emulators to test my “soon to be built” mobile website?  … I really don’t want to give up my old flip phone just so I can test my mobile website.

    Thank you for your help, it is much apreciated.

    Cheers,

    Ted

     

    1. Hello Ted,

      Thank you for your question. You can use Google Chrome Browser for for mobile emulation as described in their guide.

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

  114. Hi,

    I placed the following code into the head of one of my web pages and created a page called MobileHome.html  I want to be able to direct mobile users from that page to the new mobile page.  I tested it in screenfly but it doesn’t seem to work.  What am I doing wrong?

    Thanks,

    Ted

     

     

    <script type=”text/javascript”>

    if (screen.width<321)
    {
     window.location = “MobileHome.html”
    }

    </script>

    1. Hello Ted,

      The code you provided looks generally correct when I place it against other sample code. The only difference I see is that the URL in the other samples use the full URL (https://example.com vs example.com)

      Kindest Regards,
      Scott M

  115. Using the .htaccess file method, how would i go about adding a link back to the desktop site from the mobile version? Is it possible?

    1. Hello Grant,

      To be able to toggle mobile site to classic site you would need a bit of javascript involved as well. The javascript would set a session cookie for you while the htaccess code would evaluate that cookie to determine which version of the site to display.

      The htaccess would need to add a rewrite condition in order to handle the cookie option:

      RewriteCond %{HTTP_COOKIE} !^.*user_id=1 [NC]

      Kindest Regards,
      Scott M

  116. I have website with over 50,000 pages indexed in SE and if someone found our desktop page at below in SE.

    https://www.domain.com/sf1/sf2/sf3/sf4/sf5/index.html so how to redirect someone from desktop site page (subfolder) to mobile site page (subfolder) at https://m.domain.com/sf1/sf2/sf3/sf4/sf5/index.html

    Note: above both www. and m. are connected to same database so we don’t have to work that much to write or copy the content and have same folders name.

    We found many articles explaining how to redirectory www.domain.com redirectory to m.domain.com but just think anypage could be searched out of over 50,000 pages and it would lots of work and coding to write .htaccess for every page or subfolder and server might not handle that many .htaccess either.

    is there anyway that visitors would be redirectto the page it should be by their operating system (desktop or mobile)?

    Thanks

    1. Hello Mac,

      No, the fastest way to redirect is to do it in the .htaccess so it works with as many pages as possible. Placing it in the main .htaccess file will cascade it’s way down unless overridden by subsequent .htaccess files in the subfolders.

      One thing you may want to check into is the current trend of moving away from separate mobile pages and making the main website responsive so it works with all shapes and sizes of devices. Of course, this does not work for all sites and some still maintain the separate mobile versions. Use what is best for your situation.

      Kindest Regards,
      Scott M

  117. i m designing a website with complete bitmaps….even the background is a bitmap…if i design it in any perticular resolution the user with different resoultion finds the page setup improper…i want to cr8 a redirect link to my website versions in different resolution..plz help

     

    1. Hello amey,

      You would want to use what’s called responsive web design to detect the visitor’s browser resolution, and then you could redirect to the page designed for their resolution.

      You could also instead use the CSS @media selector to alter your site as well without doing a browser redirect and just maintaining all the code on one page:

      div#res480 { display: none; }
      div#res640 { display: none; }

      @media only screen and (min-width: 320px) and (max-width: 480px) {
      body { font-size: .8em; background-image:url(‘background-320×480.jpg’); }
      div#res480 { display: block; }
      }

      @media only screen and (min-width: 480px) and (max-width: 640px) {
      body { font-size: .8em; background-image:url(‘background-480×640.jpg’); }
      div#res640 { display: block; } }

      What is going on above is that you’d place your content for smaller screens in a <div class=”res480″;> tag for example. Then it starts out hidden along with any other different content sizes you have. Then using the @media selector it asks the browser if the page dimensions that a browser is currently showing are within a certain range and then swaps out the font-size and background-image values if so.

      Hope that helps, please let us know if you still had any questions.

      – Jacob

  118. This is probably a dumb question, but in the javascript coding method, do you leave “m.domain.com” or do you have to change it so that the “domain” portion actually reflects your website address?

    1. Hello Nina,

      Yes, the “domain” portion would need to be changed. If for example your domain was “nina.com” the redirect would be to “m.nina.com”.

      Kindest Regards,
      Scott M

  119. I would recomend just using a php command, the link on the moble site sends you to yoursite.com/?mobile=0

    and on your regular site you have:

    <?php if ($_GET[‘mobile’] !== 0) {

    echo “JAVASCRIPT HERE”;

    } ?>

    That makes it so the user can go back to the normal site and the javascript will not even be on the page anymore.

  120. Thanks, I am using a hybrid of your code and passing back the “yes” variable from my mobile site.

     

    <script type=”text/javascript”>
    <!–

            var getStr = window.location.search.substr(1);

            var getArray = getStr.split (“&”);

            var get = {};

            for ( var i = 0; i < getArray.length; i++) {

            var tmpArray = getArray[i].split(“=”);

            get[tmpArray[0]] = tmpArray[1];

            }

           if (get.m == “yes”) {

                    window.location = “https://www.mymainsite.com”

            }

            else if (screen.width <= 800) {

                    window.location = “https://mymobilesite.comi”;

            }

     //–>

      //–>
    </script>

     

    then on my mobile site links for the Full Site  I use https://www.mymainsite.com/?m=yes

    1. A bit more code needs to be added for Windows Phone. I don’t have a Windows device handy to test this with, this this should work:

      <script>
      if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/IEMobile/i.test(navigator.userAgent)){
      if(document.URL !=”https://m.example.com/products”)
      {
      window.location =”https://m.example.com/products”;
      }
      }
      </script>

    1. In the code above, the GET variable is being read via JavaScript, but it would just need to be passed within the browser. To do so, you would set your URL to something like the following for non-mobile traffic:

      https://example.com/?m=no

  121. Yes! The code needs cleaned up as the formatter here seems to have broken it up a bit, but yes, this works perfectly. It’s a snippet I use on a regular basis.

  122. You can also use PHP for this.

    <?php

     

    $isMobile = (bool)preg_match(‘#\b(ip(hone|od|ad)|android|opera m(ob|in)i|windows (phone|ce)|blackberry|tablet’.

                        ‘|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp|laystation portable)|nokia|fennec|htc[\-_]’.

                        ‘|mobile|up\.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b#i’, $_SERVER[‘HTTP_USER_AGENT’] );

     

    if($isMobile) {

        header(‘Location: ./index2.php’);

    }

     

    ?>

    1. Hi Andrew, so you’re saying if any of those regular expressions match, then redirect the user to index2.php ? … and index2.php would have to be the mobile version of your site?

  123. <script>if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){
    window.location ="https://m.domain.com";}</script>
    1. Hello Nishanth,

      Thanks for sharing that great code snippet! That would be yet another viable option to handle redirects. I just tested it on 2 mobile phones and 2 laptops and it was successful in only redirecting the phones.

      It’s worth noting that if you accessed a page at example.com/products that had this code, it would take a mobile user that might have clicked on a Google or other link to your website for your products page directly to your main mobile site page.

      You could modify the code a bit based on what file it is residing in to keep mobile users on the right pages they were trying to visit. You’d also want to make sure to check if the user is already on the mobile page to prevent a re-direct loop:

      example.com/products/index.html

      <script>
      if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){
      if(document.URL !="https://m.example.com/products")
        {
          window.location ="https://m.example.com/products";
        }
      }
      </script>

      Thanks again for sharing!

      – Jacob

      1. Hello, it’s likely going to go in the head, or in your scripts directory, but I’d suggest testing this out on a staging site if you wanted to try it since this was one contributor’s solution and we have not tested it recently. Hope it works though!

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X