URL Encoder
Make a piece of text safe to put in a link or a query string.
How it works
URL encoding, also called percent encoding, replaces unsafe characters with a percent sign and two hex digits. Letters, digits and the four marks hyphen, underscore, period and tilde are always left alone.
The important choice is what you are encoding.
If it is a single value going into a query string, such as a search phrase or a redirect address, encode everything. Characters such as &, = and ? have a job in a URL, and leaving them raw inside a value would break it. This is what JavaScript's encodeURIComponent does.
If it is a complete URL that only needs its spaces and foreign characters fixed, keep the structural characters. This is what encodeURI does.
Picking the wrong one is the most common cause of broken links with parameters.
Worked example
As a value, blue shoes & socks becomes blue%20shoes%20%26%20socks
Common URL codes
| Character | Code | Character | Code |
|---|---|---|---|
| space | %20 | / | %2F |
| ! | %21 | : | %3A |
| " | %22 | ; | %3B |
| # | %23 | < | %3C |
| $ | %24 | = | %3D |
| % | %25 | > | %3E |
| & | %26 | ? | %3F |
| ' | %27 | @ | %40 |
| ( | %28 | [ | %5B |
| ) | %29 | ] | %5D |
| * | %2A | { | %7B |
| + | %2B | | | %7C |
| , | %2C | } | %7D |
Common questions
Should a space be %20 or a plus sign?
%20 works everywhere in a URL. A plus sign only means a space inside form style query strings. When in doubt, use %20.
Do I need to encode the whole URL?
No. Encode each parameter value separately, then join them with & and =.