2

let´s say that I have a url that is parametrized to redirect to other url.

example:

http://XxxXX.net/redirect.php?param1={param1}&param2={param2}&param3={param3}&param4={param4}

and I have the following in 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>

now, the idea is that the parameters that comes from the first url calling redirect.php works to fill what I want that recirect.php do.

to then redirect to a dynamic url based on the parameters received.

is this possible?

maybe there is something wrong in my code, please help me.

Baldie47
  • 1,148
  • 5
  • 16
  • 45

2 Answers2

2

Even though this question has an accepted answer, I am providing the following solution because:

  1. The question was tagged as PHP, not JavaScript
  2. The accepted answer is not a dynamic solution as was originally requested

then redirect to a dynamic url based on the parameters received.

Example URL: http://localhost/test.php?hello=world&foo=bar

Getting the query string parameters:

$query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
parse_str($query, $result);

echo '<pre>';
print_r($result);
echo '</pre>';

Outputs:

Array
(
    [hello] => world
    [foo] => bar
)

To perform the redirect:

header('Location: http://www.example.com/?' . http_build_query($result));

This would redirect to: http://www.example.com/?hello=world&foo=bar

mister martin
  • 6,197
  • 4
  • 30
  • 63
  • I can see your solution working, and you´re right, this is PHP. but the first one is dynamic though, it created the "url" parameter based on the params that were sent originally. or am I seeing something wrong? if I sent url.com/?paramX=yyy it will redirect to example.com/?paramx=yyy which is what I need.. – Baldie47 Jul 01 '16 at 18:53
  • @gasguirre in your original question you are manually defining four variables based on the parameters, `param1`, `param2`, `param3` and `param4`. If you have to manually create those every time, it is not dynamic. My code will work with either one parameter or 50 parameters, you don't need to know how many there are before hand. – mister martin Jul 01 '16 at 19:01
  • 1
    Might want to look at `parse_str()` instead of looping. – AbraCadaver Jul 01 '16 at 19:23
  • @AbraCadaver thanks for the tip, updated my example. – mister martin Jul 01 '16 at 19:26
  • I understand now, well, in my case, I know how many parameters are going to be passed, the data that is coming from them is what varies. but now I understand. thank you. your solution really is the best here. – Baldie47 Jul 01 '16 at 22:47
1

Do you need to do the redirect in PHP?

If not, you could do window.location = url; or window.location.replace(url);

aequabit
  • 50
  • 1
  • 7
  • It's JavaScript, so if you don't especially need to do the redirect in PHP, you could do that. – aequabit Jul 01 '16 at 17:47
  • well, it is actually, right? as it´s calling to URL which was populated dynamicaly. so it works, or am I missing something? – Baldie47 Jul 01 '16 at 18:14
  • You said "redirect to a dynamic url based on the parameters receive" ...this is not dynamic because you have to know before hand how many parameters were sent.. @gasguirre – mister martin Jul 01 '16 at 18:16