In a URL, should spaces be encoded using %20 or +?

MegaByter picture MegaByter · Jul 31, 2009 · Viewed 143k times · Source

In a URL, should I encode the spaces using %20 or +? For example, in the following example, which one is correct?

www.mydomain.com?type=xbox%20360
www.mydomain.com?type=xbox+360

Our company is leaning to the former, but using the Java method URLEncoder.encode(String, String) with "xbox 360" (and "UTF-8") returns the latter.

So, what's the difference?

Answer

Greg picture Greg · Jul 31, 2009

Form data (for GET or POST) is usually encoded as application/x-www-form-urlencoded: this specifies + for spaces.

URLs are encoded as RFC 1738 which specifies %20.

In theory I think you should have %20 before the ? and + after:

example.com/foo%20bar?foo+bar