如何将用户从一个页面重定向到同一页面,但是在子域中?
http://test.com到http://subdomain.test.com
http://test.com/page/page2/123到http://subdomain.test.com/page/page2/123
https://test.com/catalog?r=44&color=red到https://subdomain.test.com/catalog?r=44&color=red
所以我想在我的例子中重定向并保存subdomain.test.com
之后的所有内容。我该怎么做?
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';
}
用这个
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;
}
您可以使用redirect() - > away(),例如redirect() - > away('http://example.com?id=2);