Suppose I want to redirect all GET queries from one site to another, using PHP.
POST queries do not exist in the system.
For example, when the user connects to
www.mysite.com/index.php?action=myAction&pageno=2
I want him to be redirected to:
www.mySecondSite.com/index.php?action=myAction&pageno=2
I can get write a hard-coded code for each and every possible $_GET variable (myAction and pageno in my example), but it does not sound like a reasonable solution. Also, I’ve seen this solution which involves modifying the configuration of the Apache server. Suppose I can’t change the server configuration.
For sure, there must be a way to get all of the variables and their values, and redirect it to another site.
First of all you need to get complete URL, for example for my local server:
Try printing
$_SERVERvariable withprint_r():You should get result like this:
For more info take a look at manual page, so now you have your URL:
And you can use
header()to redirect your request:I recommend taking look at HTTP status codes,
3xxones, whether you want to use302 Found(the default) or307 Temporary Redirect…So you can do this: