Welcome Guest, Not a member yet? Register   Sign In
subdomain is lost on redirect()->to()
#1
Bug 

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
Reply
#2

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

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); 
Reply
#4

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); 
Reply
#5

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
Reply
#6

(This post was last modified: 05-18-2020, 09:13 AM by divpusher.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB