繁体   English   中英

如何在Codeigniter中使用mobiledetect.net将mydomain.com重定向到m.mydomain.com?

[英]How to redirect mydomain.com to m.mydomain.com with mobiledetect.net in codeigniter?

如果用户通过手机打开网站,我想将mydomain.com定向到m.mydomain.com。 我使用mobiledetect.net,并且已经使用作曲家安装了它。 当我尝试通过手机打开网站时,收到错误通知“ ERR_TOO_MANY_REDIRECTS”。

这是我的控制器:

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    public function index()
    {
        $detect = new Mobile_Detect;
        if($detect->isMobile()) {
            header("location: http://m.mydomain.com");
            exit;
        }
    }
}

如何解决此错误?

我只使用CI文档中的默认htaccess

.htaccess的

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

使用移动网站时,您需要排除对移动设备的检测。 您可以这样实现:

public function index()
{
    $host='http://'.$_SERVER["HTTP_HOST"];
    $detect = new Mobile_Detect;
    if($detect->isMobile() && $host!='http://m.mydomain.com') {
        header("location: http://m.mydomain.com");
        exit;
    }
}

暂无
暂无

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

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