使用 PHP 更改 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7588304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change URL using PHP
提问by JohnA
e.g. i have page with url http://mysite.com?page=3&var=10also there is form on page.
例如,我有带有 url 的页面,页面上 http://mysite.com?page=3&var=10也有表单。
When form submitted there some actions in php but i need to remove this ?page=3&var=10after form was submitted somehow is there way compatible with all browsers trough PHP without mod_rewrite?
当表单提交时,php 中有一些操作,但我需要?page=3&var=10在表单以某种方式提交后删除它,有没有办法在没有 mod_rewrite 的情况下通过 PHP 兼容所有浏览器?
回答by Big D
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:
这是一个古老的话题,但以防万一其他人将来搜索此内容,您可以使用 javascript replaceState 更改历史记录和浏览器栏标签。一个简单的 php 函数来做到这一点:
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):
然后只需使用所需的 url 调用此函数(大概是删除 post 变量):
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.
页面重新加载或在调用另一个页面后返回现在将在历史记录中具有新的 url 位置。
I think that using POST may be a more elegant solution, but if you must use GET this is a work around.
我认为使用 POST 可能是一个更优雅的解决方案,但如果您必须使用 GET,这是一种解决方法。
回答by Tural Ali
If you're using action=index.php, then all values will be posted to index php, ?page=3&var=10will be automatically removed.
如果您正在使用action=index.php,则所有值都将发布到索引 php,?page=3&var=10将被自动删除。
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'] ?>
如果您想发布到同一页面,您可以使用 'action=index.php?page=3&var=10' 或 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
您可以在页面的开头检查是否提交了某些内容,然后使用有关标题功能的更多信息重定向到您想要的任何内容http://php.net/manual/en/function.header.phpheader('Location: http://www.example.com/');
回答by user3338098
use parse_strto get the query string as an associative array that is easy to modify. Then use http_build_queryto convert the associative array into a query string.
用于parse_str获取查询字符串作为易于修改的关联数组。然后用于http_build_query将关联数组转换为查询字符串。
$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.
preg_replace("/\\?.*/s","",$s['REQUEST_URI'])删除原始查询字符串,允许您替换它。
回答by Rene Pot
Does this work for you?
这对你有用吗?
header('Location:/');
回答by ArtoAle
Yeah, the solution is quite simple (even if not really SEO friendly):
是的,解决方案非常简单(即使不是真正的 SEO 友好):
<?php
header("Location: http://mysite.com")
?>
just for information...why do you need it?
仅供参考……你为什么需要它?
回答by Marc B
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.
mod_rewrite 不能影响用户浏览器地址栏中显示的内容,除非重写进行了外部可见的重定向。否则它只会重写网络服务器中的内容,而这对用户是不可见的。
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.
如果您想影响用户的地址栏,则必须header('Location: ...')在表单处理完成后进行重定向。

