URL encoding

Oct29
  • Share

Difficulty: ★★☆☆☆

On my job I often get a lot of questions about forming URL’s with parameters. The key is URL Encoding.
For example when you have a page which contains 2 parameters:

http://www.mypage.com/myscript.jsp?parameter1=x&parameter2=y

The parameters are seperated from the url by the question mark. “?”
The page myscript.jsp contains 2 parameters seperated each other by the “&” sign.

Now let’s say you have a forward script, a script which forwards your page to an other page.

http://www.mysite.com/forward.jsp?url=http://www.mypage.com/myscript.jsp

When you want to keep the parameters of the myscript.jsp page, you need to URL encode this.

http://www.mysite.com/forward.jsp?url=http://www.mypage.com/myscript.jsp%3Fparameter1=x%26parameter2=y

What would happen if you would not use URL encoding:
(http://www.mysite.com/forward.jsp?url=http://www.mypagecom/myscript.jsp?parameter1=x&parameter2=y)
Instead of reading the url like this:

	[1: http://www.mysite.com/forward.jsp?url= [2: http://www.mypage.com/myscript.jsp?[2A: parameter1=x]&[2B:parameter2=y]]

It breaks the URL after parameter1 because of the “&” sign, which seperates “parameter2″ from the parameter “url”:

	[1: http://www.mysite.com/forward.jsp?url= [2: http://www.mypage.com/myscript.jsp?][3: parameter1=x]&[4: parameter2=y]

Now say that you are using a monitoring tracker script. Which should track the forward script aswell.
You can simply not use an URL encoded string within an URL encoding:

http://www.thirdparty.com/tracker.jsp?url=http://www.mysite.com/forward.jsp?url=http://www.mypage.com/myscript.jsp%3Fparameter1=x%26parameter2=y

From here you are using again double questionmarks which is not right.
And the moment when the browser decodes the url, the url will be broken again on the “&” sign.

[1: http://www.thirdparty.com/tracker.jsp?url= [2: http://www.mysite.com/forward.jsp?url= [3: http://www.mypage.com/myscript.jsp?][4: parameter1=x]&[5: parameter2=y]]

Means there is no other way, to solve this in the URL. To track the forward url with url parameter, there should be a script behind, which is retrieving and tracking and forwarding the URLS.

Use this Tool to encode URLS:
http://www.xs4all.nl/~jlpoutre/BoT/Javascript/Utils/endecode.html



Leave a comment