doujie1908
doujie1908
2011-09-28 19:10

使用PHP更改URL

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 mod_rewrite?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • douge7771 douge7771 10年前

    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

    点赞 评论 复制链接分享
  • dony113407 dony113407 10年前

    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.

    点赞 评论 复制链接分享
  • doufu5401 doufu5401 5年前

    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.

    点赞 评论 复制链接分享
  • doufen3786 doufen3786 10年前

    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?

    点赞 评论 复制链接分享
  • duanchao1002 duanchao1002 10年前

    Does this work for you?

    header('Location:/');
    
    点赞 评论 复制链接分享
  • dougu2006 dougu2006 3年前

    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.

    点赞 评论 复制链接分享

为你推荐