根据发送的参数进行动态重定向


Make dynamic redirection php based on parameters sent

假设我有一个参数化的url,用于重定向到其他url。

的例子:

http://XxxXX.net/redirect.php?param1={param1}&param2={param2}&param3={param3}&param4={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+'&param3='+param3+'&param4='+param4';
  </script>
</head>
<?php    
header('Location: url');    
?>
<body>
</body></html>

现在,这个想法是来自第一个url的参数调用redirect.php工作来填补我想要的recrect .php做的

,然后根据接收到的参数重定向到一个动态url。

这可能吗?

可能是我的代码有问题,请帮助我

尽管这个问题有一个公认的答案,我还是提供以下解决方案,因为:

  1. 问题被标记为PHP,而不是JavaScript
  2. 被接受的答案不是最初请求的动态解决方案

,然后根据接收到的参数重定向到一个动态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);