简体   繁体   English

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

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

I have a php based website that I want to redirect the user to a mobile version of specific pages but only for those URL's, there are certain urls I want to keep as is for features to work as they currently do. 我有一个基于php的网站,我想将用户重定向到特定页面的移动版本,但仅针对那些URL,我想保留某些URL,以使功能按当前方式工作。

I know you can set a rule in config.php that says 我知道你可以在config.php中设置一条规则

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

But that will redirect on all pages if a mobile device is found, I just want to redirect for example if (mobile && location = "mysite/test"){//do redirect} 但是,如果找到了移动设备,它将在所有页面上重定向,例如,我只想重定向(mobile && location =“ mysite / test”){// do redirect}

Any advice on how to go about this? 关于如何解决这个问题有什么建议吗? Would a rule need to be set on each individual php page that the redirect must happen on rather than in a config.php file? 是否需要在每个必须进行重定向的单独php页面上而不是在config.php文件中设置规则?

Thanks for the help. 谢谢您的帮助。

EDIT: 编辑:

When I comment out the following then the browser does not redirect the user when on a mobile device: 当我注释掉以下内容时,浏览器在移动设备上时不会重定向用户:

/*
$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 is a lightweight PHP class for detecting mobile devices (including tablets). Mobile Detect是用于检测移动设备(包括平板电脑)的轻量级PHP类。 It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. 它结合使用User-Agent字符串和特定的HTTP标头来检测移动环境。

Using that it's quite easy to only display content for a mobile: 使用它,仅显示移动内容非常容易:

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