首页 > CMS应用 > 正文
手机访问PC网站自动跳转到手机网站及搜索引擎移动适配

在移动端访问大行其道的今天,越来越多的人都开始为自己的网站规划移动端的种种,譬如自适应或是独立子域名的移动网站,这个时候你就得考虑移动端跳转以及搜索引擎移动适配的问题了,具体解决方案如下,原作者传送门

一、仅首页跳转及适配应用

1、在电脑端网页顶部部署代码:

<!--移动端访问首页跳转到移动首页-->
<script type="text/javascript">
(function(Switch){
var switch_pc = window.location.hash;
if(switch_pc != "#pc"){
if(/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href='http://m.zhangge.net/';
}
}
})(window);
</script>
<!--百度移动适配META申明-->
<meta name="mobile-agent" content="format=xhtml;url=http://m.zhangge.net/" />
<!--谷歌、雅虎等移动META申明-->
<link href="http://m.zhangge.net/" rel="alternate" media="only screen and (max-width: 1000px)" />

2、在移动端网页顶部部署代码:

<!--非移动端访问将跳转至PC页-->
<script type="text/javascript">
(function(Switch){
var switch_mob = window.location.hash;
if(switch_mob != "#mobile"){
if(!/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href='http://zhangge.net/';
}
}
})(window);
</script>
<!--谷歌、雅虎等移动META反向申明-->
<link href="http://zhangge.net" rel="canonical" />

注意:此方法应用范围仅限于首页,但是一个网站肯定不止一个页面,因此还有必要做全站的适配

二、全跳转及适配应用

1、wordpress专用代码:将以下代码添加至主题的head.php文件,电脑端与移动端互换http://和http://m即可分别应用:

<?php
global $wp;
/*-- 获取当前页面地址 --*/
$current_url = home_url(add_query_arg(array(),$wp->request));
/*-- 将地址中的http://替换为http://m. --*/
$target_url = str_replace("http://","http://m.","$current_url");
?>
<script type="text/javascript">
(function(Switch){
var switch_pc = window.location.hash;
if(switch_pc != "#pc"){
if(/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href='<?php echo $target_url; ?>';
}
}
})(window);
</script>
<meta name="mobile-agent" content="format=xhtml;url=<?php echo $target_url; ?>" />
<link href="<?php echo $target_url; ?>" rel="alternate" media="only screen and (max-width: 1000px)" />

2、php程序通用代码
电脑端

<?php
/*-- 获取当前页面对应的移动页地址 --*/
function curMobURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://m.";$this_page = $_SERVER["REQUEST_URI"];
if (strpos($this_page, "?") !== false) $this_page = reset(explode("?", $this_page));
if ($_SERVER["SERVER_PORT"] != "80") {$pageURL .= $_SERVER["SERVER_NAME"] . ":" .$_SERVER["SERVER_PORT"] . $this_page;}
else {$pageURL .= $_SERVER["SERVER_NAME"] . $this_page;}
echo $pageURL;
}
?>

wordpress可以直接添加至function.php文件中

<script type="text/javascript">
(function(Switch){
var switch_pc = window.location.hash;
if(switch_pc != "#pc"){
if(/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href='<?php curMobURL(); ?>';
}
}
})(window);
</script>
<meta name="mobile-agent" content="format=xhtml;url=<?php curMobURL(); ?>" />
<link href="<?php curMobURL(); ?>" rel="alternate" media="only screen and (max-width: 1000px)" />

移动端

<?php
/*-- 获取当前页面对应的PC页地址 --*/
function curPcURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";$this_page = $_SERVER["REQUEST_URI"];
if (strpos($this_page, "?") !== false) $this_page = reset(explode("?", $this_page));
if ($_SERVER["SERVER_PORT"] != "80") {$pageURL .= $_SERVER["SERVER_NAME"] . ":" .$_SERVER["SERVER_PORT"] . $this_page;}
else {$pageURL .= $_SERVER["SERVER_NAME"] . $this_page;}
echo $pageURL;
}
?>
<script type="text/javascript">
(function(Switch){
var switch_mob = window.location.hash;
if(switch_mob != "#mobile"){
if(!/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href='<?php curPcURL(); ?>';
}
}
})(window);
</script>
<link href="<?php curPcURL(); ?>" rel="canonical" />

2、asp通用代码
电脑端

<%
Function GetLocationURL()
Dim Url
Dim ServerPort,ServerName,ScriptName,QueryString
ServerName = Request.ServerVariables("SERVER_NAME")
ServerPort = Request.ServerVariables("SERVER_PORT")
ScriptName = Request.ServerVariables("SCRIPT_NAME")
QueryString = Request.ServerVariables("QUERY_STRING")
Url="http://m."&ServerName
If ServerPort <> "80" Then UrlUrl = Url & ":" & ServerPort
UrlUrl=Url&ScriptName
If QueryString <>"" Then UrlUrl=Url&"?"& QueryString
GetLocationURL=Url
End Function
Response.Write GetLocationURL()
%>

移动端

<%
Function GetLocationURL()
Dim Url
Dim ServerPort,ServerName,ScriptName,QueryString
ServerName = Request.ServerVariables("SERVER_NAME")
ServerPort = Request.ServerVariables("SERVER_PORT")
ScriptName = Request.ServerVariables("SCRIPT_NAME")
QueryString = Request.ServerVariables("QUERY_STRING")
Url="http://"&ServerName
If ServerPort <> "80" Then UrlUrl = Url & ":" & ServerPort
UrlUrl=Url&ScriptName
If QueryString <>"" Then UrlUrl=Url&"?"& QueryString
GetLocationURL=Url
End Function
Response.Write GetLocationURL()
%>

3、js通用代码
电脑端

<script type="text/javascript">
(function(Switch){
var switch_pc = window.location.hash;
var thisURL = document.location.href.replace(/^http:\/\//,"http://m.");
if(switch_pc != "#pc"){
if(/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href = thisURL ;
}
}
})(window);
document.write('<meta name="mobile-agent" content="format=xhtml;url='+thisURL+'" /><link href="'+thisURL+'" rel="alternate" media="only screen and (max-width: 1000px)" />');
</script>

移动端

<script type="text/javascript">
(function(Switch){
var switch_mob = window.location.hash;
var thisURL = document.location.href.replace(/^http:\/\/m\./,"http://");
if(switch_mob != "#mobile"){
if(!/iphone|ipod|ipad|ipad|Android|nokia|blackberry|webos|webos|webmate|bada|lg|ucweb|skyfire|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile/i.test(navigator.userAgent.toLowerCase())){
Switch.location.href=thisURL;
}
}
})(window);
document.write('<link href="'+thisURL+'" rel="canonical" />');
</script>

文章评论

共有 0 条评论

Top