Spaces in URLs?

HttpUrlStandards

Http Problem Overview


w3fools claims that URLs can contain spaces: http://w3fools.com/#html_urlencode

Is this true? How can a URL contain an un-encoded space?

I'm under the impression the request line of an HTTP Request uses a space as a delimiter, being formatted as {the method}{space}{the path}{space}{the protocol}:

GET /index.html http/1.1

Therefore how can a URL contain a space? If it can, where did the practice of replacing spaces with + come from?

Http Solutions


Solution 1 - Http

A URL must not contain a literal space. It must either be encoded using the percent-encoding or a different encoding that uses URL-safe characters (like application/x-www-form-urlencoded that uses + instead of %20 for spaces).

But whether the statement is right or wrong depends on the interpretation: Syntactically, a URI must not contain a literal space and it must be encoded; semantically, a %20 is not a space (obviously) but it represents a space.

Solution 2 - Http

They are indeed fools. If you look at http://www.ietf.org/rfc/rfc3986.txt">RFC 3986 Appendix A, you will see that "space" is simply not mentioned anywhere in the grammar for defining a URL. Since it's not mentioned anywhere in the grammar, the only way to encode a space is with percent-encoding (%20).

In fact, the RFC even states that spaces are delimiters and should be ignored:

> In some cases, extra whitespace (spaces, line-breaks, tabs, etc.) may have to be added to break a long URI across lines. The whitespace should be ignored when the URI is extracted.

and

> For robustness, software that accepts user-typed URI should attempt to recognize and strip both delimiters and embedded whitespace.

Curiously, the use of + as an encoding for space isn't mentioned in the RFC, although it is reserved as a sub-delimeter. I suspect that its use is either just convention or covered by a different RFC (possibly HTTP).

Solution 3 - Http

Spaces are simply replaced by "%20" like :

http://www.example.com/my%20beautiful%20page

Solution 4 - Http

The information there is I think partially correct:

> That's not true. An URL can use spaces. Nothing defines that a space is replaced with a + sign.

As you noted, an URL can NOT use spaces. The HTTP request would get screwed over. I'm not sure where the + is defined, though %20 is standard.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichard JP Le GuenView Question on Stackoverflow
Solution 1 - HttpGumboView Answer on Stackoverflow
Solution 2 - HttpGabeView Answer on Stackoverflow
Solution 3 - HttpSandro MundaView Answer on Stackoverflow
Solution 4 - HttporlpView Answer on Stackoverflow