1. Technology

Encoding URLs

An Introduction to URL Encoding

By

What is URL Encoding?

When you pass information through a URL, you need to make sure it only uses specific allowed characters like: alphabetic characters, numerals, and a few special characters that have meaning in the URL string. Any other characters should be encoded so that they don't cause problems.

The most commonly encoded character is the <space> character. You see this character whenever you see a plus-sign (+) in a URL. This represents the space character. The plus sign acts as a special character representing a space in a URL. The most common way you'll see this is in a mailto link that includes a subject. If you want the subject to have spaces in it, you can encode them as pluses:

mailto:email?subject=this+is+my+subject

How Do You Encode a URL?

Simply replace the special characters with their encoding string. This will nearly always begin with a %. If you don't want to do it by hand, you can use a script such as is found on the JavaScript site: Encoding Web Addresses.

When Should I Encode a URL?

Strictly speaking, you should always encode any special characters found in a URL. But you generally won't find any special characters in a URL outside their normal context except with form data. If you submit data to CGI scripts using the GET method, you should encode the data as it will be sent over the URL. For instance, if you are writing a link to promote an RSS feed, your URL will need to be encoded to add to the script URL you're promoting it on.

What Should be Encoded?

Any character that is not an alphabetic character, a number or a special character that is being used outside its normal context. Below is a table of characters and their encoding. Full URL Encoding Table

Reserved Characters URL Encoding

Character Purpose in URL Encoding
: Separate protocol (http) from address %3B
/ Separate domain and directories %2F
# Separate anchors %23
? Separate query string %3F
& Separate query elements %24
@ Separate username and password from domain %40
% Indicates an encoded character %25
+ Indicates a space %2B
<space> Not recommended in URLs %20 or +

©2014 About.com. All rights reserved.