Reputation: 574
Let's say I have the following URL:
http://example.com/index.php?user/1234
What I want the PHP query to do is redirect the user to the following URL:
http://example.com/test/index.php?user/1234
Of course, the URL should not only redirect ?user/1234
but also ?anything/343
. I want to leave the url intact and only add the /test/
to it, and leave the part afterwards the same.
How do I accomplish that? All I could find is just general redirection and not specific to URLs. Thanks.
Upvotes: 2
Views: 6057
Reputation: 576
This used to be troublesome, but works for me now:
header("Location: /test/index.php?" .$_SERVER['QUERY_STRING']);
Upvotes: 0
Reputation: 574
I modified Kasia Gogolek's answer so that it works for me. This is the solution to my question:
$fullUrl = $_SERVER[REQUEST_URI];
// split the url
$url = parse_url($fullUrl);
$url['path'] = "/test" . $url['path'];
// create the new url with test in the path
$newUrl = $url['path']."?".$url['query'];
header("Location:" .$newUrl);
Upvotes: 2
Reputation: 3414
If I understand your question correctly, you need to parse your URL string and add 'test' to the path. The code below should do just that:
// $fullUrl = $_SERVER['REQUEST_SCHEME']."://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$fullUrl = "http://example.com/index.php?user/1234";
// split the url
$url = parse_url($fullUrl);
$url['path'] = "/test" . $url['path'];
// create the new url with test in the path
$newUrl = $url['scheme'] . "://".$url['host'].$url['path']."?".$url['query'];
header("Location:" .$newUrl);
Upvotes: 6
Reputation: 1998
Should be a simple header redirect
header("Location:http://example.com/test/index.php?user/1234");
If the relocation is dependant on the query, then you need to build the location url.
For example, if you 2 variables on your page you want to use, 1 that is $page
and one that is $id
, you would do it like this.
$id = 123;
$page = 'user';
header("Location:http://example.com/test/index.php?".$page."/".$id);
This would produce a url of
http://example.com/test/index.php?user/123
Upvotes: -1
Reputation: 245
You could use PHP header('location: url')
as here.
URL being your intended new destination.
Upvotes: 0