簡體   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