Mobile Redirect to Full Site and Full Site Link – .htaccess

Redirecting users from your full site to a mobile site by recognizing a device is not something that is just a simple task. It requires understanding some complicated codes and rules. There are so many different ways of attempting a mobile redirect by using anything from jquery to PHP and many others. Things can get complicated and frustrating when they do not work. However, there is a simple solution for this that really doesn’t require any knowledge of the process. The solution is by adding a simple piece of code to the .htaccess file of your server (with a little modification by you) and users will be directed to your mobile site if on a mobile browser or the full site if not.

The code is very simple. I have pasted below the full code you will need. Just copy and paste into your .htaccess file and then modify just a few lines.

1st

change this section from m.xpgraphics.org to whatever your url is of your mobile site.

RewriteRule ^ http://m.xpgraphics.org [R,L]

2nd

change the next section from http://www.xpgraphics.org to your full site url.

RewriteRule ^ – [CO=mredir:0:http://www.xpgraphics.org]

3rd

you need to add a little bit of code to your mobile website to create a link back to the full site. Place the following code at the bottom of your mobile index.html page (or whatever your mobile home page happens to be).

<a href=”www.xpgraphics.org/?m=0>Full Site</a>

4th

change the above code from http://www.xpgraphics.org to whatever your url is of your full site. Now, when user access your mobile site and click this link it will redirect to the full site without returning again to the mobile site.

Redirect Code: (add your site to bold sections)

RewriteEngine on

RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ – [CO=mredir:0:http://www.xpgraphics.org]

RewriteCond %{REQUEST_URI} !\.(jpg|css|png|gif)$ [NC]
RewriteCond %{HTTP_USER_AGENT} “android|blackberry|iphone|ipad|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile” [NC]
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]
RewriteRule ^ http://m.xpgraphics.org [R,L]

 

Advertisements
Posted in Uncategorized
19 comments on “Mobile Redirect to Full Site and Full Site Link – .htaccess
  1. idpweb says:

    Hello there. Thank you for posting this code. I followed you directions but am having some trouble with getting a server error. Would ou be able and willing to help me try to sort this out?

    Thanks!

  2. Warren says:

    Hello – and thanks for the code but I have a slight problem in that the link on the mobile device doesn’t open the desktop site – it just re-opens the mobile site. Here’s the code in my htaccess:

    RewriteRule ^ http://m.site.com [R,L]
    RewriteRule ^ – [CO=mredir:0:http://site.com%5D
    RewriteEngine on

    RewriteBase /
    RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
    RewriteRule ^ – [CO=mredir:0:http://site.com%5D

    RewriteCond %{REQUEST_URI} !\.(jpg|css|png|gif)$ [NC]
    RewriteCond %{HTTP_USER_AGENT} “android|blackberry|iphone|ipad|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile” [NC]
    RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
    RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
    RewriteRule ^ http://m.site.com [R,L]

    # addin a link to desktop site desktop site

    Any suggestions to fix??

    Many thanks

  3. Warren says:

    Thanks for all your help via email – I got it working now – much appreciated

  4. odesieus says:

    Having the same issue with defaulting back to the mobile site using the link code provided.

  5. Hi,
    I have set up a sub domain. (eg m.mydomain.com.au). When I look that up and test it in the URL field on my laptop, my mobile page is visible. So all good.

    When I add your code to the .htaccess file, i then get a Internal Error and the (m.mydomain.com.au).does not even load like it used to. as above. Or does the whole site

    I have copied the code from above exactly and just changed the domain name. Is that all you have to do.

    i have a lot of code in the .htaccess file, to speed up my site. Would that conflict with your code?

    • nicktrick says:

      It is quite possible that some of the code might conflict. If you wanted to send me an email of your .htacess file I would be more than willing to take a look at it and see if I can help.

      Thanks

  6. Mike says:

    Hi!

    Thanks for the tips. Unfortunately I also have the same problem than idpweb. I’m getting a 500 error. Could I send you my code so that you take a look at it?

    If yes, please let me know what file(s) you need.

    Thanks for the help!

  7. Mike says:

    OK thanks a lot! Just sent you an email.

    Cheers

  8. Mike says:

    I was getting a 500 Internal Server Error with your code and we couldn’t find a way to fix. I found a working solution so I thought I would share it so that it might help anyone facing the same problem than I did.

    Cheers

    COntent of the .htaccess file:

    RewriteEngine on

    RewriteBase /
    RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
    RewriteRule ^ – [CO=mredir:0:www.mysite.com]

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

    “android|blackberry|iphone|ipad|ipod|iemobile|operamobile|palmos|webos|googlebot-mobile” [NC]

    RewriteCond %{HTTP_HOST} !^m\.
    RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)
    RewriteCond %{HTTP_COOKIE} !^.*mredir=0.*$ [NC]
    RewriteRule ^ http://m.mysite.com [R,L]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: