Introduction to URL Encoding in Programming
The process of converting programming strings into URL format is known as URL Encoding. Some characters are not able to be part of a URL. For example, the space key cannot be read by a URL so it should be substituted with another character because URLs can only be sent over the internet if they use the ASCII character set.
Because URLs often contain characters that are not in the ASCII set, the URL must be converted by URL Encoding so it can meet the valid ASCII format. URL Encoding will replace unsafe ASCII characters with a “%” character along with two hexadecimal digits. These two digits are placed after the “%” character and correspond with values in the ISO-8859-1 character set. Once the URL has been encoded it is converted into a %XY string, where Y and X is a number. If you look up on the URL section of your browser, often you will see the web address with odd looking characters that do not make sense. This is the result of URL Encoding in programming.
Almost all popular web programming languages have functions that can be used to encode URLs. For example, in JavaScript you can use the encode URI() function and in PHP you can use the rawurlencode() function. These functions will automatically take a URL string and convert it into ‘safe’ characters. Various websites use different programming languages, so most of them already have built in functions to perform URL decoding and encoding.
As a general rule, and character that is non alphanumeric should be encoded. This also applies to characters that are not intended to have special meanings. In most cases, there is no harm in encoding a URL to make sure that it is compatible, even if the character does not need to be URL encoded.
