上周實驗成功手機版網站與電腦版網站的自動判斷及切換後,又把郵編庫電腦版的首頁以外的各種欄目頁面、詳細内頁也都改為了識别到移動設備後就自動跳轉到對應的郵編庫手機版頁面。
這個跳轉需要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以上才有實際意義。近期還做了一些外部鍊接,希望搜索引擎的收錄量能逐步提高。相信随着移動終端的快速發展,我們的手機版網站是會有前途的,這個郵編庫的手機版網站隻不過是一個先鋒嘗試,積累經驗便于以後的更大、更多發展。
评论