2

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.

0

5 Answers 5

6

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);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, you understood that correctly. The code isn't working quite correctly however. I get http://example.com/:///test_://example.com/index.phpuser/1234/ as the URL. So it has some wrong ` symbols, it's missing the ?` character and I get the domain name twice.
I forgot the question mark between path and query other than that it works for me.
Weird, I get the following http://example.com/:///test://example.com/index.php?user/1234. So it's some strange stuff with :// and the fact that it doesn't remove the second example.com
what is the values for your: $_SERVER['REQUEST_SCHEME'] $_SERVER[HTTP_HOST] $_SERVER[REQUEST_URI] ?
Okay, I fixed it. Thank you for your help. I posted my edit in the OP.
2

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);

Comments

0

You could use PHP header('location: url') as here.

URL being your intended new destination.

4 Comments

I don't know the new destination, it's dependent on the query
Could you be more specific? What would be different each time, the test part of the URL?
No, test would stay the same. Only the part afterwards should change. So for example, example.com/index.php?whatever should become example.com/test/index.php?whatever
You'd capture that, and append it to the new built URL. Kasia Gogolek's answer explains more.
0

This used to be troublesome, but works for me now:

header("Location: /test/index.php?" .$_SERVER['QUERY_STRING']);

Comments

-1

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

5 Comments

Yes, but what would I do if the URL doesn't have "?user/1234" but instead "?help/343"
Then you would change it to header("Location:http://example.com/test/index.php?help/343");
@OldMcDonald please be more specific about what you are trying to achieve
@OldMcDonald don't write it static, write it dynamic by putting your URL into a variable. Btw that URL format seems to be really weird...
I made my post clearer now. I want to add the /test/ part to the URL and leave everything afterwards the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.