2016-10-18 41 views
2

如何获得名为“代码”的QueryString并重定向我的页面,包括查询字符串? 例 -接收查询字符串和重定向页面

我会收到mydomain.com/inteface.php?code=103103

我需要重定向mydomain_new.com/interface.php?code=103103。

我知道C#,但在这种情况下,我将需要这个代码在PHP中重定向在不同的服务器。

+0

欢迎SO。请看[旅游](http://stackoverflow.com/tour)。您可能还想检查[我可以询问哪些主题](http://stackoverflow.com/help/on-topic)以​​及[如何提出一个好问题](http://stackoverflow.com/help/)如何提问)和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/),以及如何创建[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。发布您尝试过的代码以及收到的错误。尽可能具体,因为它会导致更好的答案。 –

回答

0

您可以使用名为$_GET & $_SERVER超全局变量。您可以使用$_GET['code']从当前网址和$_SERVER['HTTP_HOST']code变量抢域这样的:

 //Grab code from URL 
     $code = $_GET['code']; 

     //Grab current Domain Name being used 
     $currentURL = $_SERVER['HTTP_HOST']; 

     //Old Domain Name 
     $oldDomain = "mydomain.com"; 


     //Read the header of the URL to test $domain is TRUE and $code has data 
     if ($currentURL == $oldDomain && isset($code)) { 

       //Redirect to new domain using $_GET 
       header('Location: http://mydomain_new.com/interface.php?code=$code');//No need to concatenate single variable 

     } 

参见:

http://php.net/manual/en/reserved.variables.get.php

http://php.net/manual/en/reserved.variables.server.php

+0

谢谢,它的工作很好Kitson88。 –

+0

@EdilsonRafael不用担心,很高兴它为你工作! – Kitson88

1
header('Location:mydomain_new.com/interface.php?code='.$_GET['code']); 
+0

谢谢!这对我来说是Pradyut的工作。 –