php 带有HTTP查询字符串变量的php重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4702149/
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
php redirect with HTTP query string variables
提问by Graslandpinguin
atm I'm using the following four lines to redirect the user to another page on my website:
atm 我使用以下四行将用户重定向到我网站上的另一个页面:
<?php
header("Status: 301 Moved Permanently");
header("Location: ./content/index.html");
exit;
?>
but there is a problem with the use of HTTP query string variables like http://< url >?param=blah
they don't get appended to url understandably.
但是使用 HTTP 查询字符串变量存在问题,例如http://< url >?param=blah
它们没有被附加到 url 可以理解。
Is there a smart move for implementing this?
是否有实现这一点的明智之举?
greetings
你好
回答by regality
<?php
header("Status: 301 Moved Permanently");
header("Location:./content/index.html?". $_SERVER['QUERY_STRING']);
exit;
?>
回答by jontro
To do the redirect and make sure an extra question mark does not show up when query string is not passed use the following
要进行重定向并确保在未传递查询字符串时不显示额外的问号,请使用以下命令
function preserve_qs() {
if (empty($_SERVER['QUERY_STRING']) && strpos($_SERVER['REQUEST_URI'], "?") === false) {
return "";
}
return "?" . $_SERVER['QUERY_STRING'];
}
header("Status: 301 Moved Permanently");
header("Location: ./content/index.html" . preserve_qs());
回答by jave.web
I would like to add one more option...
Anyways - like others have said, using (.htaccess) mod_rewritewould probably be the best option.
我想再添加一个选项......
无论如何 - 就像其他人所说的那样,使用(.htaccess)mod_rewrite可能是最好的选择。
However,
there surely can be many situations when you have to do it in PHP => you have 2 options:
但是,
当您必须在 PHP 中执行此操作时,肯定会有很多情况 => 您有 2 个选项:
- Append your redirection URI with
$_SERVER['QUERY_STRING']
- Append your redirection URI with a query built on your own:
http_build_query($_GET);
- 附加您的重定向 URI
$_SERVER['QUERY_STRING']
- 使用您自己构建的查询附加您的重定向 URI :
http_build_query($_GET);
Option 2. - Advantages (+)
选项 2. - 优势 (+)
- Encodes the parameters ( default by PHP_QUERY_RFC1738)
- you can easily add (or remove) some
$_GET
params like:$_GET['new_param']="new value";
(add)unset($_GET['param_to_remove']);
(remove) - if environment (god knows why) does not provide a QUERY_STRING- you are probably still able to get the superglobal
$_GET
=> environmentaly independent http_build_query()
's 1st param can actually be ANY array or object so you can build a$_GET
-like request from$_POST
or$o = new YourObject();
if necessary- you can change argument separator if necessary
- 对参数进行编码(默认为PHP_QUERY_RFC1738)
- 您可以轻松添加(或删除)一些
$_GET
参数,例如:($_GET['new_param']="new value";
添加)unset($_GET['param_to_remove']);
(删除) - 如果环境(天知道为什么)不提供QUERY_STRING- 您可能仍然能够获得超全局
$_GET
=> 环境独立 http_build_query()
的第一个参数实际上可以是任何数组或对象,因此您可以$_GET
从$_POST
或$o = new YourObject();
在必要时构建类似请求- 如有必要,您可以更改参数分隔符
Option 2. - Dissadvantages (-)
选项 2. - 缺点 (-)
- this kind of building query might be redundant ("good-for-nothing"), just unnecessary...
- if the query is big enaugh (maybe some attack?) it could have an affect on performace, because everytime there will be an array converted to a string & encoded
- 这种构建查询可能是多余的(“无用的”),只是不必要的......
- 如果查询足够大(也许是一些攻击?)它可能会对性能产生影响,因为每次都会有一个数组转换为字符串并编码
For more info see http://www.php.net/manual/en/function.http-build-query.php- a PHP manual's site about the http_build_query()
function which Returns a URL-encoded string.
有关更多信息,请参阅http://www.php.net/manual/en/function.http-build-query.php- 有关该http_build_query()
函数的 PHP 手册站点Returns a URL-encoded string.
回答by eklundchristopher
Using $_SERVER['QUERY_STRING']
and appending it to the end of your redirect might be what you're looking for.
使用$_SERVER['QUERY_STRING']
它并将其附加到重定向的末尾可能就是您要查找的内容。
EDIT:Just noticed I was a bit late with my reply.
编辑:刚刚注意到我的回复有点晚了。
回答by Crayon Violent
First off why not redirect with mod rewrite?
首先,为什么不使用 mod rewrite 重定向?
But anyways, you can concat $_SERVER['QUERY_STRING'] to the end of your url
但无论如何,您可以将 $_SERVER['QUERY_STRING'] 连接到您的网址的末尾
回答by dasrecht
I would also suggest to use mod_rewritefor this task, you can be more flexible.
我还建议将mod_rewrite用于此任务,您可以更灵活。
回答by Alec
Add the $_SERVER['REQUEST_URI']
or QUERY_STRING
. Do a print_r($_SERVER);
to see more information about the requested URL.
添加$_SERVER['REQUEST_URI']
或QUERY_STRING
。执行 aprint_r($_SERVER);
以查看有关请求的 URL 的更多信息。