在重定向之前更改URL中的字母

I'm trying to grab the current pages URL and change some values in it before redirecting to the next page with header()

If my current url is http://example.com/abc-def-1-xxx-g.php I want to change the xxx part to a so my url will then be http://example.com/abc-def-1-a-g.php

Heres what I have for the header on the xxx page before redirect

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

This should work for you:

$str = "http://example.com/abc-def-1-xxx-g.php";
$url = str_replace("xxx", "a", $str);

//For example
echo $url;

Output:

http://example.com/abc-def-1-a-g.php

EDIT:

To grab the current URL use this:

$str = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Also don't forget to use exit() after each header so you are sure the script stops to be executed like this:

header("Location: $url");
exit();

Try this:

$actual_link = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$url = str_replace("xxx", "a", $actual_link); 
Header("Location: ".$url);

You can easily parse the string before you call header().

Try parse_str, str_replace, etc.

And you have both $_SERVER['QUERY_STRING'] and $_SERVER['REQUEST_URI'] available, depending on which you need to parse:

$qs = parse_str(str_replace("$string_I_do_not_want",'',$_SERVER['REQUEST_URI']));

if ($some_value_from_string == $something_to_change) {

    $new_value = compute_my_new_value();
}
$new_location = "http://example.com/". $somepage . $my_qs_with_new_value;
header("location: $new_location");
exit;