0

How can I redirect user from one page to the same page, but in subdomain?

http://test.com to http://subdomain.test.com

http://test.com/page/page2/123 to http://subdomain.test.com/page/page2/123

https://test.com/catalog?r=44&color=red to https://subdomain.test.com/catalog?r=44&color=red

So I want redirect and save all what present after subdomain.test.com in my example. How can I do it?

public function test($request)
    {
        $subdomain = Market::first()->subdomain;
        $domain = $this->get_domain($request);
        $protocol = $this->get_request_protocol($request);

        $to = $protocol . '://' . $subdomain . '.' . $domain; // all path and params?

        return Redirect::to($to);

    }

    private function get_domain($request)
    {
        $parts = explode('.', $request->getHost());
        $i = count($parts)-1;

        return implode('.', [$parts[$i-1], $parts[$i]]);
    }

    private function get_request_protocol($request)
    {
        if ($request->secure()) {
            return 'https';
        }

        return 'http';
    }
Teretto
  • 635
  • 2
  • 10
  • 22

2 Answers2

1

use this

private function get_back_path($url, $domain, $subdomain)
{
    $parts = parse_url($url);

    $url = $parts['scheme'] . '://' . $subdomain . '.' . $domain;
    if (isset($parts['path'])) {
        $url .= $parts['path'];
    }
    if (isset($parts['query'])) {
        $url .= '?' . $parts['query'];
    }
    if (isset($parts['fragment'])) {
        $url .= $parts['fragment'];
    }

    return $url;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
autumnrustle
  • 595
  • 1
  • 10
  • 21
0

You can use redirect()->away() like redirect()->away('http://example.com?id=2);

Ateeq
  • 39
  • 7