CodeIgniter Forums
subdomain is lost on redirect()->to() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: subdomain is lost on redirect()->to() (/thread-76475.html)



subdomain is lost on redirect()->to() - divpusher - 05-15-2020

Hello!

I love CI4, so far so good! Heart

I set up my site to handle subdomains as languages, e.g.:

mydomain.com
de.mydomain.com
fr.mydomain.com

The problem is when I redirect, it loses the subdomain, for example :

I'm on the page de.mydomain.com
I send a form, the controller redirects with this code:
Code:
return redirect()->to('/search');

I arrive to mydomain.com/search instead of de.mydomain.com/search


RE: subdomain is lost on redirect()->to() - michalsn - 05-15-2020

You have to use full URL here. Otherwise CI will use baseURL from App config file as default domain.


RE: subdomain is lost on redirect()->to() - kick - 05-15-2020

Have you tried using
PHP Code:
current_url() 

which will return the full url (including segments that you can strip out) in your redirect statement?

So it could possibly look like this:

PHP Code:
$url =  str_replace("/unwanted/segments""/search"current_url());

retrun redirect()->to($url);

//or if you have no segments

$url current_url() . "/search";

return 
redirect()->to($url); 



RE: subdomain is lost on redirect()->to() - divpusher - 05-16-2020

Thanks for the replies.

Kick: current_url() leaves the subdomain out as well.

I managed to solve it using the full path like this:

PHP Code:
return redirect()->to('https://' $_SERVER['HTTP_HOST'] . '/search'); 


However, I think the default behavior of the to() function should be to use the full URL, including the subdomain. And maybe put a second optional boolean argument if you want to redirect to the domain without the subdomain part, e.g.:

PHP Code:
// you are on https://de.mydomain.com/

// takes you to https://de.mydomain.com/search
return redirect()->to('/search');

// takes you to https://mydomain.com/search
return redirect()->to('/search'true); 



RE: subdomain is lost on redirect()->to() - ARAmiss - 05-18-2020

You can define baseURL property in App config (Config/App) like this:
PHP Code:
    public function __construct()
    {
        parent::__construct();

        $this->baseURL 'https://' $_SERVER['HTTP_HOST'] . '/';
    }

    public $baseURL null



RE: subdomain is lost on redirect()->to() - divpusher - 05-18-2020

Thanks! It's a nice workaround and I don't have to rewrite all the to() and back() functions! BTW I tracked down the issue to current_url() function, that's where the subdomain goes missing.

(05-18-2020, 04:33 AM)ARAmiss Wrote: You can define baseURL property in App config (Config/App) like this:
PHP Code:
    public function __construct()
    {
        parent::__construct();

        $this->baseURL 'https://' $_SERVER['HTTP_HOST'] . '/';
    }

    public $baseURL null