开发者

Change URL using PHP

开发者 https://www.devze.com 2023-04-08 04:55 出处:网络
e.g. i have page with urlhttp://mysite.com?page=3&var=10 also there is form on page. When form submitted there some actions in php but i need to remove this ?page=3&var=10 after form was subm

e.g. i have page with url http://mysite.com?page=3&var=10 also there is form on page.

When form submitted there some actions in php but i need to remove this ?page=3&var=10 after form was submitted somehow is there way compatible with all browsers trough PHP without mo开发者_C百科d_rewrite?


This is an old topic, but just in case anyone else is searching for this in the future, you can use the javascript replaceState to change the history and browser bar label. A simple php function to do this:

function set_url( $url )
{
    echo("<script>history.replaceState({},'','$url');</script>");
}

Then would simply call this function with the desired url (presumably dropping the post variables):

set_url("http://example.com");

A page reload or a back after calling another page will now have the new url location in the history.

I think that using POST may be a more elegant solution, but if you must use GET this is a work around.


If you're using action=index.php, then all values will be posted to index php, ?page=3&var=10 will be automatically removed.

If you want to post to the same page you can either use 'action=index.php?page=3&var=10' or action=<?php echo $_SERVER['PHP_SELF'] ?>

You can check at the beginning of the page if something submitted and then redirect to whatever you want with header('Location: http://www.example.com/'); More about header function http://php.net/manual/en/function.header.php


Yeah, the solution is quite simple (even if not really SEO friendly):

<?php
 header("Location: http://mysite.com")
?>

just for information...why do you need it?


use parse_str to get the query string as an associative array that is easy to modify. Then use http_build_query to convert the associative array into a query string.

$queryString = $s['QUERY_STRING'];
$params = array();
parse_str($queryString, $params);
//change $params as needed
$queryString = http_build_query($params);
if ($queryString) {
  $queryString = '?'.$queryString;
}
return preg_replace("/\\?.*/s","",$s['REQUEST_URI']).$queryString;

preg_replace("/\\?.*/s","",$s['REQUEST_URI']) removes the original query string allowing you to replace it.


Does this work for you?

header('Location:/');


mod_rewrite cannot affect what's displayed in the user's browser address bar, UNLESS the rewrite does an externally visible redirect. Otherwise it only rewriting things within the webserver, and that's invisible to the user.

If you want to affect the user's address bar, you'll have to do a redirect via header('Location: ...') after the form's finished processing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号