上周实验成功手机版网站与电脑版网站的自动判断及切换后,又把邮编库电脑版的首页以外的各种栏目页面、详细内页也都改为了识别到移动设备后就自动跳转到对应的邮编库手机版页面。
这个跳转需要PHP程序来实现,去年找到MediaWiki网站页面中嵌入PHP代码的办法起到了关键作用,我修改了省份、城市、区县、地区、地址、邮编、区县邮编这七种页面使用的模板,在模板中嵌入各自对应的程序名称:
- 省份:<include_PHP file="redirect_province" />
- 城市:<include_PHP file="redirect_city" />
- 区县:<include_PHP file="redirect_county" />
- 地区:<include_PHP file="redirect_area" />
- 地址:<include_PHP file="redirect_address" />
- 邮编:<include_PHP file="redirect_postcode" />
- 区县邮编:<include_PHP file="redirect_postcode_" />
在这7个PHP程序中对浏览终端进行判断,并且对来源页面的URL进行识别,当终端为移动设备并且URL是正常需要切换的页面时,就跳转到对应的手机版本页面去。例如redirect_province.php中的代码:
<?php
#$mobileRedirect="http://m.youbianku.com/";
#$pcRedirect="http://www.youbianku.com/%E9%A6%96%E9%A1%B5";
$server="http://m.youbianku.com";
$path="/province";
$mobile_browser = '0';if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|ios|mobi)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}/*
echo "HTTP_USER_AGENT = ".$_SERVER['HTTP_USER_AGENT']."<br />\n"
. "HTTP_ACCEPT = ".$_SERVER['HTTP_ACCEPT']."<br />\n"
. "HTTP_X_WAP_PROFILE = ".$_SERVER['HTTP_X_WAP_PROFILE']."<br />\n"
. "HTTP_PROFILE = ".$_SERVER['HTTP_PROFILE']."<br />\n"
. "ALL_HTTP = ".$_SERVER['ALL_HTTP']."<br />\n";
*/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-','Googlebot-Mobile');
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;
}$uri_pc=$_SERVER["REQUEST_URI"];
$hanzi_pc=substr(urldecode($uri_pc),1);
$redirect=1;
switch ($hanzi_pc) {
case "北京":
case "北京市":
$hanzi_mobile="北京市";//beijing
break;
case "天津":
case "天津市":
$hanzi_mobile="天津市";//tianjin
break;
case "河北":
case "河北省":
$hanzi_mobile="河北省";//hebei
break;
case "内蒙古":
case "内蒙古自治区":
$hanzi_mobile="内蒙古自治区";//neimenggu
break;
case "山西":
case "山西省":
$hanzi_mobile="山西省";//shanxi
break;
case "上海":
case "上海市":
$hanzi_mobile="上海市";//shanghai
break;
case "安徽":
case "安徽省":
$hanzi_mobile="安徽省";//anhui
break;
case "江苏":
case "江苏省":
$hanzi_mobile="江苏省";//jiangsu
break;
case "浙江":
case "浙江省":
$hanzi_mobile="浙江省";//zhejiang
break;
case "山东":
case "山东省":
$hanzi_mobile="山东省";//shandong
break;
case "江西":
case "江西省":
$hanzi_mobile="江西省";//jiangxi
break;
case "福建":
case "福建省":
$hanzi_mobile="福建省";//fujian
break;
case "广东":
case "广东省":
$hanzi_mobile="广东省";//guangdong
break;
case "广西":
case "广西壮族自治区":
$hanzi_mobile="广西壮族自治区";//guangxi
break;
case "海南":
case "海南省":
$hanzi_mobile="海南省";//hainan
break;
case "河南":
case "河南省":
$hanzi_mobile="河南省";//henan
break;
case "湖北":
case "湖北省":
$hanzi_mobile="湖北省";//hubei
break;
case "湖南":
case "湖南省":
$hanzi_mobile="湖南省";//hunan
break;
case "黑龙江":
case "黑龙江省":
$hanzi_mobile="黑龙江省";//heilongjiang
break;
case "吉林":
case "吉林省":
$hanzi_mobile="吉林省";//jilin
break;
case "辽宁":
case "辽宁省":
$hanzi_mobile="辽宁省";//liaoning
break;
case "陕西":
case "陕西省":
$hanzi_mobile="陕西省";//shaanxi
break;
case "甘肃":
case "甘肃省":
$hanzi_mobile="甘肃省";//gansu
break;
case "宁夏":
case "宁夏回族自治区":
$hanzi_mobile="宁夏回族自治区";//ningxia
break;
case "青海":
case "青海省":
$hanzi_mobile="青海省";//qinghai
break;
case "新疆":
case "新疆维吾尔自治区":
$hanzi_mobile="新疆维吾尔自治区";//xinjiang
break;
case "重庆":
case "重庆市":
$hanzi_mobile="重庆市";//chongqing
break;
case "四川":
case "四川省":
$hanzi_mobile="四川省";//sichuan
break;
case "云南":
case "云南省":
$hanzi_mobile="云南省";//yunnan
break;
case "贵州":
case "贵州省":
$hanzi_mobile="贵州省";//guizhou
break;
case "西藏":
case "西藏自治区":
$hanzi_mobile="西藏自治区";//xizang
break;
default:
$hanzi_mobile="";
$redirect=0;
}
$uri_mobile="/".urlencode($hanzi_mobile);if (strpos($uri,"/index.php")!==FALSE) {
$redirect=0;
}$mobileRedirect=$server.$path.$uri_mobile;
if($mobile_browser>0 AND $redirect==1) {
// echo "mobile version";
header("Location: $mobileRedirect"); //手機版
}else {
// echo "pc version";
// header("Location: $pcRedirect"); //電腦版
}
?>
做了上面的工作后,可以看到手机版本的流量有明显增加,从以前的每日200左右增加到2000左右,不过依然太少了,广告收入也可以忽略不计,还需要上升到10000以上才有实际意义。近期还做了一些外部链接,希望搜索引擎的收录量能逐步提高。相信随着移动终端的快速发展,我们的手机版网站是会有前途的,这个邮编库的手机版网站只不过是一个先锋尝试,积累经验便于以后的更大、更多发展。
评论