这是我用在dede织梦程序上的代码,所以只要是php写的应该都可以用
<?php
这段放手机站里
header("Cache-Control: no-cache");
header("Pragma: no-cache");
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(symbianos|android|iphone|ucweb|blackberry)/i";
if($ua != '' && !preg_match($uachar, $ua)){
echo 'window.location.href="http://www.domain.com";';
exit();
}
?>
<?php
//这段放在电脑网站里
header("Cache-Control: no-cache");
header("Pragma: no-cache");
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(symbianos|android|iphone|ucweb|blackberry)/i";
if($ua != '' && preg_match($uachar, $ua)){
echo 'window.location.href="http://http://m.domain.com/";';
exit();
}
?>
JS的写法
<script>
var browser_class = navigator.userAgent;
var browser_class_name1 = browser_class.match("Mobile");
var browser_class_name2 = browser_class.match("mobile");
var location_url = window.location.href;
if (browser_class_name1 != null || browser_class_name2 != null){
if (location_url.match("wap") == null){
window.location.href="http://m.domain.com/";
}
} else
{
if (location_url.match("3g") != null || location_url.match("wap") != null){
window.location.href="http://m.domain.com/";
}
}
</script>