繁体   English   中英

如果移动设备和网址=特定网址,如何重定向

[英]how to redirect if mobile and url = specific url

我有一个基于php的网站,我想将用户重定向到特定页面的移动版本,但仅针对那些URL,我想保留某些URL,以使功能按当前方式工作。

我知道你可以在config.php中设置一条规则

if(!defined('_MOBILE') && !defined('_ADMIN') && $_SESSION['DV'] != 'true' && $_REQUEST['code'] == '' && $converting != 'true') {
        header("Location:".$config['MOBILE_URL'].$vinf);
        exit;
    }

但是,如果找到了移动设备,它将在所有页面上重定向,例如,我只想重定向(mobile && location =“ mysite / test”){// do redirect}

关于如何解决这个问题有什么建议吗? 是否需要在每个必须进行重定向的单独php页面上而不是在config.php文件中设置规则?

谢谢您的帮助。

编辑:

当我注释掉以下内容时,浏览器在移动设备上时不会重定向用户:

/*
$config['GLOBAL_ENV'] = (strpos(php_sapi_name(), 'cgi')) ? 'env -i ' : NULL;
$config['MOBILE_URL'] = $config['base_url'].'/mobile';
if ($config['mobile_force_redirect'] == '1') {
    $config['IPHONE'] = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    if (strpos($_SERVER['HTTP_USER_AGENT'],"iPad")) $config['IPHONE'] = strpos($_SERVER['HTTP_USER_AGENT'],"iPad");
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (stripos($ua,"android")) $config['IPHONE'] = stripos($ua,"android");
    if($config['IPHONE']) {
    if(!defined('_MOBILE') && !defined('_ADMIN') && $_SESSION['DV'] != 'true' && $_REQUEST['code'] == '' && $converting != 'true') {
        header("Location:".$config['MOBILE_URL'].$vinf);
        exit;
    }
    }
}*/

Mobile Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。 它结合使用User-Agent字符串和特定的HTTP标头来检测移动环境。

使用它,仅显示移动内容非常容易:

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

// Check for any mobile device.
if ($detect->isMobile() && $_SERVER['REQUEST_URI'] == "YOUR_URL_HERE"){
    header("Location:".$config['MOBILE_URL'].$vinf);
    exit;
}
else
   // other content

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM