[英]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.