首先,您需要获取完整的URL,例如我的本地服务器:
http://localhost/server_var.php?foo=bar&second=something
尝试使用print_r()打印$_SERVER变量:
print_r( $_SERVER);
你应该得到这样的结果:
Array
(
...
[REQUEST_URI] => /server_var.php?foo=bar&second=something
...
)
有关详细信息,请查看manual page,现在您有了以下网址:
$url = 'http://www.mySecondSite.com' . $_SERVER['REQUEST_URI'];
您可以使用header()重定向您的请求:
header( 'Location: ' . $url);
我建议你看一下HTTP status codes,3xx,无论你是想使用302 Found(默认)还是307 Temporary Redirect …
The second special case is the “Location:” header. Not only does it
send this header back to the browser, but it also returns a REDIRECT
(302) status code to the browser unless the 201 or a 3xx status code
has already been set.
所以你可以这样做:
header('HTTP/1.0 307 Temporary Redirect');
header( 'Location: ' . $url);