简体   繁体   English

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

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

I want to direct mydomain.com to m.mydomain.com if the user opens the website via mobile. 如果用户通过手机打开网站,我想将mydomain.com定向到m.mydomain.com。 I use mobiledetect.net and I have installed it using a composer. 我使用mobiledetect.net,并且已经使用作曲家安装了它。 when I try to open a website via my mobile I get an error notification "ERR_TOO_MANY_REDIRECTS". 当我尝试通过手机打开网站时,收到错误通知“ ERR_TOO_MANY_REDIRECTS”。

This my controller : 这是我的控制器:

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

how can I fix this error? 如何解决此错误?

I only use the default htaccess from the CI documentation 我只使用CI文档中的默认htaccess

HTACCESS .htaccess的

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

you need to exclude detection of mobile, when you are using the mobile site. 使用移动网站时,您需要排除对移动设备的检测。 You can achieve this like this: 您可以这样实现:

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.

相关问题 Laravel 4.1使用301永久重定向将所有请求从www.mydomain.com重定向到mydomain.com - Laravel 4.1 Redirect all requests from www.mydomain.com to mydomain.com with 301 permanent redirect 在Heroku上将app.mydomain.com作为mydomain.com/app运行 - Run app.mydomain.com as mydomain.com/app on Heroku Symfony 为 mydomain.com/parameter 创建路由 - Symfony create routing for mydomain.com/parameter 网址重写:将mydomain.com/index.php更改为mydomain.com/home - URL rewriting: Change mydomain.com/index.php to mydomain.com/home PHP邮件未从mydomain.com发送到Google应用程序电子邮件myname@mydomain.com - PHP mail is not sending from mydomain.com to the google apps email myname@mydomain.com 使用PHP / Nginx将cookie域从无域迁移到.mydomain.com - Migrate cookie domain from no domain to .mydomain.com with PHP/Nginx 如何使用zend框架获取像mydomain.com/username这样的动态URL - how to get dynamic URL like mydomain.com/username using zend framework 如何调试“ Waiting For mydomain.com” Web请求陷入等待状态 - How to debug “Waiting For mydomain.com” web-request getting stuck at pending state 提交表单后,将数据插入Mysql并将用户重定向到URL mydomain.com/entries/last_inserted_id - After form submit, insert data into Mysql and redirect user to an url mydomain.com/entries/last_inserted_id 从www.mydomain.com/foo重定向到www.mydomain.com/foo/ - Redirect from www.mydomain.com/foo to www.mydomain.com/foo/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM