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").

Redirecting to a mobile version?

If you think about how your redirect actually works, in theory it's quite understandable - "If the user is using a mobile device, redirect them to the mobile version of the site". The problem is that when people visit your website, their web browser does not know whether, "I am a mobile device" or "I am a desktop computer". Instead, the browser introduces itself by providing a "user-agent", which includes the browser name, version, and other information about your operating system.

To redirect a mobile visitor, you'll need to determine if their browser is a web browser on a mobile device or not. You can determine which browser the visitor is using. You can also look at other things such as their screen resolution and whether they accept file types that are common to mobile devices. Below we've listed a few methods you can use to redirect visitors to the mobile version of your website.

Using Plugins to redirect

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

Javascript window.location Method

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

You can use the following code to do this:

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

Note! While testing this on a SAMSUNG smart phone, the screen resolution varied based upon how the phone was held. To get the best results, you may have to test with various smart phones.

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

.htaccess URL rewrite redirects

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

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

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

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Support Center Login


Social Media Login

Related Questions

Here are a few questions related to this article that our customers have asked:
about mobile and desktop site redirection
abou desktop redirection from mobile(switch to desktop view)
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
2013-05-11 1:22 pm
You have just saved my life. I used the Javascript window.location Method but did not place it in the header. I placed it in the article itself and added this as well <script type="text/javascript" src="media/lib/jquery-1.8.2.min.js"></script>. It worked like a charm. It's a joomla site I was building and wanted to redirect users for a single page based on the device they using. You are a star. Thanks
2013-07-05 2:37 pm
This is great, but how can give the user an option to return to the full site, overriding the mobile via a link on the mobile site.
Staff
9,968 Points
2013-07-05 4:19 pm
Hello stephenbrust, and thanks for the question.

It would be a bit tricky to handle a redirection back to the full desktop view of the website. It would really depend on which method you're utilizing, and if you're doing this on a custom coded website, or using a CMS like WordPress or Joomla which would typically use plugins to manage these types of things.

If you have your mobile and desktop sections setup under a different sub-domain then you could use a link on your mobile pages to force the full view to be used. One way to attack this type of issue would be using JavaScript to read the GET variables that are passed, and on your mobile version of the site you could create a link to ?m=no to indicate you'd like to force the full desktop view:

<head>
<title>Title</title>
<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 = "http://m.example.com"
}
else if (get.m == "no") {
window.location = "http://www.example.com";
}

//-->
</script>
</head>


Hopefully that can help steer you in the right direction, let us know what software you're using if you'd like a more specific example.

- Jacob
2013-07-17 10:19 pm
JacobIMH couldn't you just make one index page to redirect users to either the mobile site or desktop site and then have links to the second page on each site? (index -> "desktop home" or "Mobile Home" ; then link to those home pages)
Staff
9,968 Points
2013-07-18 3:11 pm
Hello crollur, and thanks for the comment.

I think what you're saying is have a main index page that provides a link to either the desktop or mobile version of the site, and then on both versions of the site include in the menu the ability to jump to the alternative version?

That definitely would be an option, but in the context of this article wouldn't handle the automatic mobile redirection of your visitors via either JavaScript or .htaccess.

Thanks again for adding another option to the mix!

- Jacob
n/a Points
2014-11-22 1:51 am

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 = "http://clwalker.ddns.net/mobile/index.html";} else {    x = window.location = "http://clwalker.ddns.net/index2.html";}  }  //--></script>----popup will cancel to full desktop site .... ok will continue to mobileworked for me :)thanks to all of you for the help!

n/a Points
2015-01-29 3:04 pm

which java script i  have to use toredirect the mobile website

Staff
11,884 Points
2015-01-29 3:13 pm
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
n/a Points
2014-04-12 5:23 am
<script>if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location ="http://m.domain.com";}</script>
Staff
9,968 Points
2014-04-12 10:24 am
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 !="http://m.example.com/products")
{
window.location ="http://m.example.com/products";
}
}
</script>


Thanks again for sharing!

- Jacob

n/a Points
2014-05-25 8:04 pm

Hello. Works great except windows phone. What is missing in the code? Thanks

Staff
10,819 Points
2014-05-27 8:27 am
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 !="http://m.example.com/products")
{
window.location ="http://m.example.com/products";
}
}
</script>

n/a Points
2014-08-29 11:51 am

It does except the second to last / needs to be a |Thanks for the code, works a treat!

n/a Points
2014-10-01 11:01 pm

Well done job!

n/a Points
2014-05-13 12:20 am

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');

}

 

?>

Staff
5,399 Points
2014-05-13 10:51 am
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?
n/a Points
2014-05-13 10:59 am

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.

n/a Points
2014-05-15 2:44 am

HI Jacob, could you show a demo for the "get=yes/no" example?

Staff
10,819 Points
2014-05-15 7:42 am
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:

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

n/a Points
2014-06-03 2:42 pm

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 = "http://www.mymainsite.com"        }        else if (screen.width <= 800) {                window.location = "http://mymobilesite.comi";        } //-->  //--></script>

 

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

n/a Points
2014-06-16 11:58 pm

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.

n/a Points
2015-01-10 1:11 pm

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 = "http://m.singleglow.com";} else {    x = window.location = "http://singleglow.com";}  }  //--></script>";

 

} ?>

 

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

Staff
22,045 Points
2015-01-11 10:58 am
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
n/a Points
2014-07-07 11:54 am

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?

Staff
22,045 Points
2014-07-07 12:35 pm
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
n/a Points
2014-07-11 9:44 am

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

 

Staff
10,819 Points
2014-07-11 10:27 am
Could you provide a link to the website that you are referring to?
Staff
9,968 Points
2014-07-13 3:58 am
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('/support/background-320x480.jpg'); }
div#res480 { display: block; }
}

@media only screen and (min-width: 480px) and (max-width: 640px) {
body { font-size: .8em; background-image: url('/support/background-480x640.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
n/a Points
2014-07-17 2:40 pm
it was great ! Now www.****.co will be mobile
n/a Points
2014-07-27 1:53 pm

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

http://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 http://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

Staff
22,045 Points
2014-07-27 6:50 pm
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
n/a Points
2014-08-01 6:01 am

Its a good script

https://github.com/sebarmeli/JS-Redirection-Mobile-Site

 

 

n/a Points
2014-08-02 1:51 am

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?

Staff
22,045 Points
2014-08-04 9:21 am
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

n/a Points
2014-08-12 12:32 am

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>

Staff
22,045 Points
2014-08-12 9:26 am
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 (http://example.com vs example.com)

Kindest Regards,
Scott M
n/a Points
2014-08-12 10:00 pm

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

 

Staff
11,884 Points
2014-08-13 10:11 am
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
n/a Points
2014-08-24 9:01 am

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

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

n/a Points
2014-08-28 7:56 pm

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

n/a Points
2014-08-31 3:50 pm

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!!

Staff
10,819 Points
2014-09-02 1:35 pm
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.
n/a Points
2014-09-12 12:06 am

i'm hosting website through blogger how can i redirect it into mobile version?plz i need ur help

Staff
22,045 Points
2014-09-12 9:14 am
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="http://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
n/a Points
2014-09-21 5:57 pm

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 : http://www.example.com they should be redirected to http://www.example.com/mobile now this page also has a link for mobile users to see the full site version thats http://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

Staff
22,045 Points
2014-09-22 7:49 am
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
n/a Points
2014-10-02 3:50 pm

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 :)

Staff
10,819 Points
2014-10-02 3:51 pm
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.
n/a Points
2014-10-09 3:54 pm

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 !="http://m.example.com/products"){window.location ="http://m.example.com/products";}}</script>

Staff
19,888 Points
2014-10-09 4:57 pm
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.
n/a Points
2014-10-14 1:22 pm

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 = "http://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: http://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!

 

 

Staff
10,819 Points
2014-10-14 1:31 pm
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:

http://example.com?m=no

n/a Points
2014-10-14 4:13 pm

 

 That doesn't seem to work for me: 

http://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 ?)

 

 

n/a Points
2014-11-14 1:52 pm

Dave,

I had the same problem with iphones not redirecting. Just remove the www from your url and it works.

Staff
10,819 Points
2014-10-14 4:41 pm
The code needs to run on your main site and needs to redirect to the main site. The link would be the following:
http://www.1777americanainn.com?m=no
n/a Points
2014-10-14 5:01 pm

Sorry, I meant to say my redirect link on the mobile site is: http://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 !="http://www.1777americanainn.com/mobilesite")

{

window.location ="http://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 = "http://www.1777americanainn.com/mobilesite"

        }

        else if (get.m == "no") {

                window.location = "http://www.1777americanainn.com";

        }

 

 

 //-->

</script>

On the mobile site I have the lnk:

<li><a href="http://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!

 

   

 

 

 

Staff
10,819 Points
2014-10-14 5:30 pm
The problem is caused because you have 2 scripts there. Remove the first one and you should be good to go.
n/a Points
2014-10-14 6:17 pm

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.

Staff
10,819 Points
2014-10-14 6:33 pm
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 !="http://www.1777americanainn.com/mobilesite")

{
if (get.m != "no") {
window.location ="http://www.1777americanainn.com/mobilesite";
}
}

}
//-->

</script>

n/a Points
2014-10-14 7:32 pm

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.  

Staff
10,819 Points
2014-10-14 7:45 pm
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.
n/a Points
2014-10-14 8:26 pm

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.

Staff
10,819 Points
2014-10-14 8:28 pm
When I look at your home page, it looks like you do not have this new redirect in there. Have you changed it?
n/a Points
2014-10-14 10:06 pm

Do you mean the modified version of the script you gave me? It's right under the Google Analytics script.

Staff
19,888 Points
2014-10-15 8:54 am
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.
n/a Points
2014-10-15 11:15 am

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?  

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

Thanks for your help!

Staff
19,888 Points
2014-10-15 1:22 pm
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 !="http://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.
n/a Points
2014-10-15 1:57 pm

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?

 

 

Staff
19,888 Points
2014-10-15 3:02 pm
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.
n/a Points
2014-10-15 3:42 pm

Ok, I've made those changes but still no luck!

 

 

Staff
19,888 Points
2014-10-15 4:22 pm
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.
n/a Points
2014-10-15 4:49 pm

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

Staff
19,888 Points
2014-10-15 4:53 pm
Hello Dave,

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

Kindest regards,
Arnel C.
n/a Points
2014-10-16 12:36 pm

Hi Arnel,

 

Just checking to see if you got that .htaccess file I sent you.

 

Staff
19,888 Points
2014-10-16 1:35 pm
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
n/a Points
2014-10-16 1:55 pm

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 ^(.*)$ http://www.1777americanainn.com/$1 [R=301,L,NC]

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

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

 

RewriteRule ^rooms.html http://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>

 

 

Staff
11,884 Points
2014-10-16 4:00 pm
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 ^ http://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:
http://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 http://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
n/a Points
2014-10-16 4:39 pm

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 ="http://www.1777americanainn.com/mobilesite";

}

}

//-->

</script>

 

 

 

Staff
11,884 Points
2014-10-16 4:44 pm
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
n/a Points
2014-10-16 6:02 pm

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 ^ http://m.example.com [R,L]

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

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

3. I removed the apostrophies in the words "we're" and "haven't" - does that sound right?

 

Staff
11,884 Points
2014-10-16 7:05 pm
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
n/a Points
2014-10-17 8:05 am

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

Staff
10,819 Points
2014-10-17 9:03 am
There are several solutions provided within this article. Are you having trouble with a particular step?
n/a Points
2014-10-24 8:20 pm
Putting practical solutions like this that you offered
n/a Points
2014-10-31 12:35 am

Thank you very much !!!

n/a Points
2014-11-07 11:00 am

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

Staff
628 Points
2014-11-07 11:29 am
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
n/a Points
2014-11-09 8:57 pm

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 info@amandasmithphotography.com. 

Staff
11,884 Points
2014-11-10 2:03 pm
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
n/a Points
2014-11-14 3:22 pm

Hi.

 

I was wandering, is it possible to redirect mobile traffic after a few seconds? 

Staff
22,045 Points
2014-11-14 3:44 pm
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
n/a Points
2014-11-26 12:15 am
it is very very helpful for me. thankssss....
n/a Points
2014-12-02 12:45 am
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#)
Staff
11,884 Points
2014-12-11 10:26 am
Hello Bob,

Thank you for your question. I found a post online via search, where they provide a possible solution for pulling a visitor's IP, and search for the location.

You can also view some visitor information in AWSTATS, or by using Google Webmaster tools.

Thank you,
John-Paul
n/a Points
2014-12-10 11:22 pm

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 :)

Staff
11,884 Points
2014-12-11 10:29 am
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
n/a Points
2014-12-12 4:59 am

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="http://www.m.lescabanesdepyrene.com/?m=1";}</script></head>
 

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

Thanks!!

Staff
10,819 Points
2014-12-12 10:05 am
It appears that there is additional code on the mobile site that is causing the redirect. How was the mobile site developed?
n/a Points
2014-12-12 12:07 pm

I also tried this code:

<script type="text/javascript">    if (screen.width <= 800) {    window.location = "http://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.

Staff
10,819 Points
2014-12-12 12:52 pm
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.
n/a Points
2014-12-14 11:10 pm
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).
n/a Points
2014-12-15 3:55 pm

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 ^(.*)$ http://mobile.blownawayconference.ca/ [R,L]
##END MOBILE

 

Staff
22,045 Points
2014-12-15 4:17 pm
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
n/a Points
2014-12-16 11:46 pm

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!

Staff
11,884 Points
2014-12-17 10:49 am
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
n/a Points
2014-12-17 9:32 pm

Thank you Mr. John-Paul!

n/a Points
2014-12-18 9:54 pm

How do i access my website's ".htaccess" file and make those changes above?

Staff
11,884 Points
2014-12-18 10:12 pm
Hello Darwin,

Thank you for contacting us again. There are several ways to edit files on your server, they are covered in our guide How to edit a file on my server.

Also, we have a helpful guide with additional information on your .htaccess file.

Thank you,
John-Paul
n/a Points
2014-12-19 5:26 am

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 ^(.*) http://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 ^ http://m.home.com [R,L]

 

Please help me with the code for new redirects.

Thanks

Somesh.

Staff
22,045 Points
2014-12-19 9:09 am
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
n/a Points
2014-12-26 5:51 pm

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

Staff
628 Points
2014-12-26 6:11 pm
Hello Mike,

I found a list of all of the LG chocolate user agents. It looks to be the Polaris user agent.

Kindest Regards,
TJ Edens
n/a Points
2014-12-27 5:00 pm

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... ??

Staff
22,045 Points
2014-12-29 10:48 am
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
n/a Points
2015-01-12 5:32 pm

<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 !="http://www.m.nrgelectric.ca")

 

{

if (get.m != "no") {

window.location ="http://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

n/a Points
2015-01-19 8:31 pm

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 !="http://mobile.dudamobile.com/site/pringroofing3")

{

window.location ="http://mobile.dudamobile.com/site/pringroofing3";

}

}

</script>

Staff
22,045 Points
2015-01-19 9:20 pm
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
n/a Points
2015-01-19 9:59 pm

thanks Scott I will try that, i will let you know what happens

n/a Points
2015-01-26 5:25 am

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 http://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?

Staff
19,888 Points
2015-01-26 11:08 am
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.
n/a Points
2015-01-26 1:10 pm

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 = "http://www.mobilesite.com";
  }

if (get.m != "no") {
window.location ="http://www.fullsite.com";
}
  //-->
</script>
Staff
19,888 Points
2015-01-26 1:31 pm
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.
n/a Points
2015-01-26 1:41 pm

Okay I updated this is what I have now

<script type="text/javascript">

  <!--

  if (screen.width <= 800) {

    window.location = "http://www.mobile.com";

  }

 

if (get.m = "no") {

window.location ="http://www.main.com";

}

  //-->

</script>

 

And my link on my mobile to go back to my full page is "http://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

Staff
22,045 Points
2015-01-26 2:53 pm
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 ="http://www.mobile.com";

}
else if (screen.width <= 800) {

window.location = "http://www.mobile.com";

}
//-->

</script>

Kindest Regards,
Scott M
n/a Points
2015-01-26 5:24 pm

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??

Staff
19,888 Points
2015-01-26 6:10 pm
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.
n/a Points
2015-01-27 10:05 am

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 ="http://www.mainsite.com";

 

}

else if (screen.width <= 800) {

 

window.location = "http://www.mobilesite.com";

 

}

//-->

 

</script>

Staff
22,045 Points
2015-01-27 10:28 am
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
n/a Points
2015-01-27 10:38 am

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.

Staff
22,045 Points
2015-01-27 12:06 pm
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
n/a Points
2015-01-27 3:26 pm

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.

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

Staff
10,819 Points
2015-01-27 3:37 pm
While rel=canonical does still help in many cases, Google has changed their views on mobile since they first implemented rel=canonical and has started cracking down on mobile sites and generally prefers responsive sites. Further information may be found at the following:

The Definitive Guide to Google's New Mobile SEO Rules
n/a Points
2015-01-26 12:48 pm

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 !="http://www.mobilesite.com"){if (get.m != "no") {window.location ="http://www.mainsite.com";}}}//--></script>

Staff
22,045 Points
2015-01-26 3:11 pm
Hello Tim,

The last part of the code reads a bit odd, it should not be:
if (get.m != "no") {window.location ="http://www.mainsite.com";}

But rather:
if (get.m != "no") {window.location ="http://www.mobile.com";}


It basically reads "If get.m is NOT 'no' then go to the mobile site."

Kindest Regards,
Scott M
n/a Points
2015-01-27 10:15 am

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 = "http://wwww.mainsite.com/content"        }    

     else if (screen.width <= 800) {                

 window.location = "http://www.mobilesite.com";     

    } //-->  //--></script>

 

Then my code on the mobile site is this:

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

Staff
10,819 Points
2015-01-27 12:09 pm
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 = "http://mainsite";
} else if (screen.width <= 800) {
window.location = "http://mobilesite";
}
//-->
</script>

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

137 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!