重定向正常工作的问题php

I have a little problem with redirecting. Registered users follows this link site.com/reg.php?passkey=1234 but the first the user get redirected to the correct language based on a cookie. I need to keep the passkey variable when the user is redirected. like this ?lang=en_US&passkey=1234

My code so far look something like this:

if (!isset($_GET['lang']))
{

        if (isset($_COOKIE['country'])) 
        {

                $country = $_COOKIE['country'];

                (...)

                elseif  ( $country == "US" ){       

                $variables = $_GET;
                $variables['lang'] = "en_US";

                header('Location: ?' . http_build_query($variables));

                exit();
                }   

This works:

reg.php
reg.php?lang=en_US
reg.php?lang=en_US&passkey=test
reg.php?passkey=test&lang=en_US

but this gives an The page isn't redirecting properly error

reg.php?passkey=test

I don't understand why this doesn't work when all the other combinations seem to work perfectly.

The HTTP 1.1 Specification requires that the location needs to be a Absolute URI (See RFC2616 14.30 Location)

The location header('Location: ?' . http_build_query($variables)); does not contain an absolute URI.

You need something like:

header('Location: /folder/file.php?'.http_build_query($variables));

If you need to do this on different locantions you can use $_SERVER['PHP_SELF'] to set the current file as redirect location. For example

header('Location: '.$_SERVER['PHP_SELF'].'?'.http_build_query($variables));

I think, you should change the http_build_query($variables) to http_build_query($variables, null, '&')

I hope my answer is useful.