As a developer you might want to redirect your web version of the site to mobile version. As we know that all of us get’s quite busy with our day to day schedules, but we really want to be connected with world via internet on the move, and while leaving in the era of mobile devices it is all possible now a days. You simply have to follow very simple steps to redirects any user to the mobile version form web version of your website or vice versa:
Redirection can be done from both sides:
1. Redirect user to Mobile Version.
2. Redirect user to Web Version.
1. Redirect from Web Version to Mobile Version:
Navigate to header.php of your Web version & place the following code in <head> section:
<script language="javascript">
var query = window.location.search.substring(1);
var redirect = getQueryStringParameterByName(query, "redirect");
if (redirect!="temp")
{
var redirectagent = navigator.userAgent.toLowerCase();
var redirect_devices = ['vnd.wap.xhtml+xml', 'sony', 'symbian', 'nokia', 'samsung', 'mobile', 'windows ce', 'epoc', 'opera mini', 'nitro', 'j2me', 'midp-', 'cldc-', 'netfront', 'mot', 'up.browser', 'up.link', 'audiovox', 'blackberry', 'ericsson', 'panasonic', 'philips', 'sanyo', 'sharp', 'sie-', 'portalmmm', 'blazer', 'avantgo', 'danger', 'palm', 'series60', 'palmsource', 'pocketpc', 'smartphone', 'rover', 'ipaq', 'au-mic', 'alcatel', 'ericy', 'vodafone', 'wap1', 'wap2', 'teleca', 'playstation', 'lge', 'lg-', 'iphone', 'android', 'htc', 'dream', 'webos', 'bolt', 'nintendo'];
for (var i in redirect_devices)
{
if (redirectagent.indexOf(redirect_devices[i]) != -1)
{
location.replace("http://mobile.yoursitename.com");
}
}
}
function getQueryStringParameterByName(queryString, paramName)
{
tokens = queryString.split("&");
if (tokens!=null)
{
for (i=0;i<tokens.length;i++)
{
ft = tokens[i].split("=");
if (ft[0] == paramName)
{
if (ft[1].indexOf("%20",0)>0)
{
var returnValue = ft[1].replace("%20"," ");
returnValue = returnValue.replace("%20"," ");
return returnValue;
}
return ft[1];
}
}
return "";
}
}</script>
In the above code we’ve used location.replace(“http://mobile.yoursitename.com”);
You can replace “http://mobile.yoursitename.com” by your mobile site URL.
2. Redirect from Mobile Version to Web Version:
Add the following code in footer.php of your mobile version of the site:
<a target="_blank" href="http://yoursitename.com/index.php?redirect=temp">Full Site</a>
In the above code we’ve used “http://yoursitename.com/index.php”. You can replace that by the name of your site(Web Version).
What are you waiting for?? Employ the above code and do the redirection.