Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having two ? in a URL - Is it valid?

Here is the thing:

Let's say I have a base URL like this:

www.mysite.com?my_first_param=1&my_second_param=2

But now I want to add a new parameter to my URL:

www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com

Imagine that I am using a service where they add new tracking parameters to my URL. For example:

first_tracking=value1&second_value=value2

Since my BASE url already has a "?" the parameters are added with a "&", so my final URL would look like this:

www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com&first_tracking=value1&second_value=value2

This is a correct URL with several parameters, but when I do the redirect to www.myothersite.com, since the parameters start with a "&", they get lost. Would it be correct to add the tracking parameters with a starting "?" ? Like this:

www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com?first_tracking=value1&second_value=value2

If not, what would be a good approach for dealing with this? I believe is responsibility of the redirect to pass through the tracking parameters to the redirect URL.

like image 726
Nobita Avatar asked Nov 03 '22 06:11

Nobita


1 Answers

You should URLencode each param name and value properly, so if the map of params you want is this:

my_first_param => 1
my_second_param => 2
my_redirect_site => www.myothersite.com?first_tracking=value1&second_value=value2

then you should pass this as the query string:

?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com%3Ffirst_tracking%3Dvalue1%26second_value%3Dvalue2

You should be using a library which does this already to build up URIs to handle this encoding for you.

like image 169
Yuliy Avatar answered Nov 14 '22 01:11

Yuliy