2 added 15 characters in body
source | link

You can use the below function which makes use of parse_url and http_build_url:

function wprdcv_param_redirect() {
    //Prevent infinite loop
    if ($_GET["redirected"] == "1") return;

    $url_parts = parse_url("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    parse_str($url_parts['query'], $params);

    $params["redirected"] = "1";
    foreach (array_keys($_GET) as $n) {
        $params[$n] = $_GET[$n];
    }

    // Note that this will url_encode all values
    $url_parts['query'] = http_build_query($params);
    wp_redirect(http_build_url($url_parts));
    exit;
}
add_action('template_redirect', 'wprdcv_param_redirect');  

You can use the below function which makes use of parse_url and http_build_url:

function wprdcv_param_redirect() {
    //Prevent infinite loop
    if ($_GET["redirected"] == "1") return;

    $url_parts = parse_url("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    parse_str($url_parts['query'], $params);

    $params["redirected"] = "1";
    foreach (array_keys($_GET) as $n) {
        $params[$n] = $_GET[$n];
    }

    // Note that this will url_encode all values
    $url_parts['query'] = http_build_query($params);
    wp_redirect(http_build_url($url_parts));
}
add_action('template_redirect', 'wprdcv_param_redirect');  

You can use the below function which makes use of parse_url and http_build_url:

function wprdcv_param_redirect() {
    //Prevent infinite loop
    if ($_GET["redirected"] == "1") return;

    $url_parts = parse_url("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    parse_str($url_parts['query'], $params);

    $params["redirected"] = "1";
    foreach (array_keys($_GET) as $n) {
        $params[$n] = $_GET[$n];
    }

    // Note that this will url_encode all values
    $url_parts['query'] = http_build_query($params);
    wp_redirect(http_build_url($url_parts));
    exit;
}
add_action('template_redirect', 'wprdcv_param_redirect');  
1
source | link

You can use the below function which makes use of parse_url and http_build_url:

function wprdcv_param_redirect() {
    //Prevent infinite loop
    if ($_GET["redirected"] == "1") return;

    $url_parts = parse_url("http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    parse_str($url_parts['query'], $params);

    $params["redirected"] = "1";
    foreach (array_keys($_GET) as $n) {
        $params[$n] = $_GET[$n];
    }

    // Note that this will url_encode all values
    $url_parts['query'] = http_build_query($params);
    wp_redirect(http_build_url($url_parts));
}
add_action('template_redirect', 'wprdcv_param_redirect');