假设我有一个参数化的url,用于重定向到其他url。
的例子:
http://XxxXX.net/redirect.php?param1={param1}¶m2={param2}¶m3={param3}¶m4={param4}
在redirect。php
中有以下内容<html><head>
<script>
function getURLParameter(name) {
return decodeURI(
(RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
);
}
</script>
<script>
var param1 = getURLParameter('param1');
var param2 = getURLParameter('param2');
var param3 = getURLParameter('param3');
var param4 = getURLParameter('param4');
var url = 'http://xxxx.xxxxx.net/yyyy/'+param1+'?param2='+param2+'¶m3='+param3+'¶m4='+param4';
</script>
</head>
<?php
header('Location: url');
?>
<body>
</body></html>
现在,这个想法是来自第一个url的参数调用redirect.php工作来填补我想要的recrect .php做的
,然后根据接收到的参数重定向到一个动态url。
这可能吗?
可能是我的代码有问题,请帮助我
尽管这个问题有一个公认的答案,我还是提供以下解决方案,因为:
- 问题被标记为PHP,而不是JavaScript
- 被接受的答案不是最初请求的动态解决方案
,然后根据接收到的参数重定向到一个动态url。
示例URL: http://localhost/test.php?hello=world&foo=bar
获取查询字符串参数:
$query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
parse_str($query, $result);
echo '<pre>';
print_r($result);
echo '</pre>';
输出:
Array
(
[hello] => world
[foo] => bar
)
重定向:
header('Location: http://www.example.com/?' . http_build_query($result));
这将重定向到:http://www.example.com/?hello=world&foo=bar
你需要在PHP中做重定向吗?
如果没有,您可以使用window.location = url;
或window.location.replace(url);