【问题标题】:How to redirect mydomain.com to m.mydomain.com with mobiledetect.net in codeigniter?如何在codeigniter中使用mobiledetect.net将mydomain.com重定向到m.mydomain.com?
【发布时间】:2019-12-05 13:45:54
【问题描述】:

如果用户通过移动设备打开网站,我想将 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]

【问题讨论】:

  • 请出示您的 .htaccess 或 windows 等效文件
  • 我只使用 CI 文档中的默认 htaccess

标签: php codeigniter


【解决方案1】:

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

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;
    }
}

【讨论】:

  • 这行得通。但在直接访问 m.mydomain.com 之前,错误消息仍然会出现大约几秒钟。不直接到 m.mydomain.com
  • 这可能是缓存问题,换个手机浏览器或其他手机试试
  • 我尝试了不同的设备并删除了浏览器缓存,但仍然如此
  • 我添加了'http://'来构建 $host 变量。现在它应该在第一次尝试时匹配。在它寻找匹配 m.mydomain.com 之前,它没有(因此出现错误)然后重定向到 http://m.mydomain.com 并匹配。现在它应该排在第一位。请确认。
  • 这行得通 :) 你能告诉我,如何让这个脚本在所有控制器上运行吗??
猜你喜欢
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 2016-04-07
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多