Question by justbill: How do I fix this html code for a mobile redirect in WordPress?
Rich now I have my site set up with the regular site in the main root domain (www.mysite.com) and the mobile version in a subdomain (m.mysite.com). I have the main wordpress site in a folder named “wordpress” on my server and I altered the index.php with the code below, but it currently can only redirect to http://m.mysite.com no matter what the person typed in to begin with i.e. if they enter www.mysite.com/contact on their phone, they get redirected to m.mysite.com and not m.mysite.com/contact
Is there any way to fix this so they get redirected to the duplicate mobile version of whatever URL they enter but at the m. subdomain?
I got the instructions for the redirect here: http://mobility.mobi/showthread.php?22-Auto-detecting-PC-Mobile-and-then-Redirecting
and here’s the index.php code in root folder:
$ mobile_browser = '0';
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
strtolower($ _SERVER['HTTP_USER_AGENT']))){
$ mobile_browser++;
}
if((strpos(strtolower($ _SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
((isset($ _SERVER['HTTP_X_WAP_PROFILE']) or isset($ _SERVER['HTTP_PROFILE'])))){
$ mobile_browser++;
}
$ mobile_ua = strtolower(substr($ _SERVER['HTTP_USER_AGENT'],0,4));
$ mobile_agents = array(
‘w3c ‘,’acs-’,'alav’,'alca’,'amoi’,'audi’,'avan’,'benq’,'bird’,'blac’,
‘blaz’,'brew’,'cell’,'cldc’,'cmd-’,'dang’,'doco’,'eric’,'hipt’,'inno’,
‘ipaq’,'java’,'jigs’,'kddi’,'keji’,'leno’,'lg-c’,'lg-d’,'lg-g’,'lge-’,
‘maui’,'maxo’,'midp’,'mits’,'mmef’,'mobi’,'mot-’,'moto’,'mwbp’,'nec-’,
‘newt’,'noki’,'oper’,'palm’,'pana’,'pant’,'phil’,'play’,'port’,'prox’,
‘qwap’,'sage’,'sams’,'sany’,'sch-’,'sec-’,'send’,'seri’,'sgh-’,'shar’,
‘sie-’,'siem’,'smal’,'smar’,'sony’,'sph-’,'symb’,'t-mo’,'teli’,'tim-’,
‘tosh’,'tsm-’,'upg1′,’upsi’,'vk-v’,'voda’,'wap-’,'wapa’,'wapi’,'wapp’,
‘wapr’,'webc’,'winw’,'winw’,'xda’,'xda-’);
if(in_array($ mobile_ua,$ mobile_agents)){
$ mobile_browser++;
}
if (strpos(strtolower($ _SERVER['ALL_HTTP']),’OperaMini’)>0) {
$ mobile_browser++;
}
if (strpos(strtolower($ _SERVER['HTTP_USER_AGENT']),’windows’)>0) {
$ mobile_browser=0;
}
if($ mobile_browser>0){
header(‘Location: http://m.mysite.com/’);
} else {
/**
* Front to the WordPress application. This file doesn’t do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define(‘WP_USE_THEMES’, true);
/** Loads the WordPress Environment and Template */
require(‘./wordpress/wp-blog-header.php’);
}
Best answer:
Answer by Josh L
Hey,
Your problem seems fairly complex and technical. Perhaps you should get some real-time advice from WordPress expert? Some interactive conversation should def help you to solve your problems quicker.
Nicholas Butler probably is the right person for you to talk to.
https://www.minutebox.com/profiles/loudmouthman
What do you think? Answer below!