4

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.

Community
  • 1
  • 1
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104

3 Answers3

4

First of all you need to get complete URL, for example for my local server:

http://localhost/server_var.php?foo=bar&second=something

Try printing $_SERVER variable with print_r():

print_r( $_SERVER);

You should get result like this:

Array
(
    ...
    [REQUEST_URI] => /server_var.php?foo=bar&second=something
    ...
)

For more info take a look at manual page, so now you have your URL:

$url = 'http://www.mySecondSite.com' . $_SERVER['REQUEST_URI'];

And you can use header() to redirect your request:

header( 'Location: ' . $url);

I recommend taking look at HTTP status codes, 3xx ones, whether you want to use 302 Found (the default) or 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.

So you can do this:

header('HTTP/1.0 307 Temporary Redirect');
header( 'Location: ' . $url);
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • @yes123 I've provided my source in answer... It's "w3c on HTTP codes" they claim that `307 Temporary Redirect`, `302 Found`... (click on link for HTTP status codes please) and I personally find w3.org more reliable than wiki. – Vyktor Feb 19 '12 at 11:14
  • the difference between the 2 is pretty subtile.. i would stick with 302 personally for a temp redirect – dynamic Feb 19 '12 at 11:18
  • @yes123 I don't change it neither, but it was meant as "If you are interested you can get more info here", for case he will need it in future. + for permanent redirects (301) it's worth knowing that there are more statuses than just 302. Again. Was meant as study material :) – Vyktor Feb 19 '12 at 11:23
  • 1
    @Andrey I recommend studying all `$_SERVER` subvariables (and keys), for case you won't need whole `REQUEST_URI` or so :) – Vyktor Feb 19 '12 at 11:36
4

Write this php code at the top of the index.php page before your html tag.

<?
$newUrl = "www.mySecondSite.com/index.php";
if (!empty($_GET)){
$parameter = $_GET;
$firstTime = true;
foreach($parameter as $param => $value){
    if ($firstTime) {
        $newUrl .= "?" . $param . "=" . $value;
        $firstTime = false;
    } else {
        $newUrl .= "&" . $param . "=" . $value;
    }
}
}
header("Location: ".$newUrl);
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
Nantu
  • 557
  • 1
  • 4
  • 16
  • Just out of curiosity, is there a reason you're not using `http_build_query($_GET)` instead of your loop? – Hobo Sep 19 '14 at 15:42
2

Redirecting in .htaccess would be the way to go, but if you say you can't change that you can insert a conditional header with a 301 code in your index.php file: http://php.net/manual/en/function.header.php

You have the contents of the query string available in $_SERVER['QUERY_STRING']

okyanet
  • 3,106
  • 1
  • 22
  • 16