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";

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

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

Like this Article?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
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.
9,962 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:

<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";


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)
9,962 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-04-12 5:23 am
window.location ="http://m.domain.com";}</script>
9,962 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:

if(document.URL !="http://m.example.com/products")
window.location ="http://m.example.com/products";

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

8,135 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:

if(document.URL !="http://m.example.com/products")
window.location ="http://m.example.com/products";

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




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?

8,135 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:


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


} ?>

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

17,220 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


8,135 Points
2014-07-11 10:27 am
Could you provide a link to the website that you are referring to?
9,962 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.bisu.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)?


17,220 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




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?

17,220 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


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?





<script type="text/javascript">

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


17,220 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.




8,310 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,

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


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. 





Post a Comment

Email Address:
Phone Number:

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

34 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


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!