Mobile Redirect to Full Site (.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 www.xpgraphics.org to your full site url.
RewriteRule ^ – [CO=mredir:0: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 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: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]
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!
Yeah sure. What seems to be the problem?
I’m getting a 500 error. I’ve remove the .htaccess file for now and will resume working on it tomorrow.
Is ther an email I can use to contact you?
Thanks for the help!
Yeah its nrwilliams@xpgraphics.org. If you wanna send me the code or something that’s fine. I’ll try and help best I can.
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]
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteRule ^ – [CO=mredir:0:http://site.com]
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
Can you give me the site so I can test it out? What are you running before the rewrite engine is turned on?
Thanks
Sure – but would it be okay to email it to you rather than post it in public (if you’re okay sharing your email address here?)
Yeah its nrwilliams@xpgraphics.org
Thanks for all your help via email – I got it working now – much appreciated
Having the same issue with defaulting back to the mobile site using the link code provided.
Could you provide the code specifically that you used on your site?