CodeIgniter Forums
Redirecting to mobile url issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Redirecting to mobile url issue (/showthread.php?tid=64842)



Redirecting to mobile url issue - waptik - 03-31-2016

Hi guys. I have an issue with redirecting mobile users to mobile url.

Define('MOBILE_URL_RAW', 'm.mysite.tld');

in __construct() of core/MY_Controller.php i have this following code:

if($this->detect->isMobile()){
$url = parse_url($_SERVER['REQUEST_URI']);
if($url["host"] != MOBILE_URL_RAW){

redirect(str_replace(MOBILE_URL_RAW, $url["host"], $url));
}
}
i also tried:
if($this->detect->isMobile() && $_SERVER["HTTP_HOST"] != MOBILE_URL_RAW){

redirect(str_replace(MOBILE_URL_RAW.$_SERVER['REQUEST_URI']);
}

but it seems not to work because the requested page keeps refreshing till it says 'couldnt get page' .
How to make it work?


RE: Redirecting to mobile url issue - waptik - 04-01-2016

so nobody to help me out? Not even a single push for me?


RE: Redirecting to mobile url issue - PaulD - 04-01-2016

I can't say I have an answer but this might be a problem.

Quote:'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.

So parse_url will return something like
Code:
Array ( [path] => /index.html ) 1

So you pass this test here:
Code:
if($url["host"] != MOBILE_URL_RAW){

And do this command
Code:
redirect(str_replace(MOBILE_URL_RAW, $url["host"], $url));

And since MOBILE_URL_RAW is not found, you are redirecting back to the original calling url.

I think the logic is flawed, but am suprised you do not get an error thrown, so perhaps I am wrong.

Hope  that helps,

Paul.

PS Perhaps something like: (not tested or used, just playing about as bored at mo)

PHP Code:
Define('MOBILE_URL_RAW''m.mysite.tld');

if(
$this->detect->isMobile()){
       if(
$_SERVER['REQUEST_HOST'] != MOBILE_URL_RAW){
              
redirect(MOBILE_URL_RAW.'/'.$_SERVER['PATH_INFO']);
       }