Can I replace % 20 with & nbsp in URLs that have spaces?

25,732

Solution 1

The short answer is, they are both used to represent "spaces", but they represent different spaces.

%20 is the URL escaping for byte 32, which corresponds to plain old space in pretty much any encoding you're likely to use in a URL.

  is an HTML character reference which actually refers to character 160 of Unicode (and also ISO-8859-1 aka Latin-1). It's a different space character entirely -- the "non-breaking space". Even though they look pretty much the same, they're different characters and it's unlikely that your server will treat them the same way.

Solution 2

No. Neither are spaces (technically). Both represent spaces in different ways though. Make every effort to NOT have spaces, or representatives of spaces, in your URLs. Many find it more elegant (me included) to replace spaces with _ or -

Solution 3

No.   is an HTML non-breaking-space entity; this entity has no meaning when used in a filesystem or wherever else that a URL might point. URLs are not encoded in HTML.

Solution 4

No, not in the URLs. What you can do is replace spaces in the textual representation of the URL.

So instead of:

<a href="http://some.site/doc%20with%20spaces">http://some.site/doc%20with%20spaces</a>

you can have:

<a href="http://some.site/doc%20with%20spaces">http://some.site/doc&nbsp;with&nbsp;spaces</a>

Solution 5

%20 is what you get with URL encoding, so this is what you should use if you are going to use it in a URL.

&nbsp; is a HTML entity, which is what should be used for 'non breaking space' in an HTML document.

Share:
25,732
Pup
Author by

Pup

Extremely terse.

Updated on July 09, 2022

Comments

  • Pup
    Pup almost 2 years

    Within my HTML, can I use the character entity reference "&nbsp;" in place of "%20" in Web URLs?

    They're both spaces, right?

  • Tomalak
    Tomalak almost 15 years
    Does anyone know why the &nbsp; that I have in the lower sample does not show up? It used to, I'm sure...
  • Nick Lewis
    Nick Lewis almost 15 years
    Because it was turned into a space! Use &amp;nbsp; for it to show up properly.
  • Tomalak
    Tomalak almost 15 years
    Snap! You really think that's the cause? ;-) Seriously - I thought that a "code" section was to display it's contents verbatim. And I thought it did that until recently. Now if I write &amp;nbsp;, I fear that this will show up at some point...
  • Ben Blank
    Ben Blank almost 15 years
    Depending on your point of view, that's not quite correct. %20 represents a space, but &nbsp; represents a non-breaking space, technically a separate character. So even if it were a good idea to use HTML escaping in URLs, this wouldn't work because it would replace one character with another, changing the meaning of the URL.
  • Laurence Gonsalves
    Laurence Gonsalves almost 15 years
    The server doesn't speak HTML, but he was talking abut using this in his HTML. The browser does interpret character references in attributes, including URLs, and so the &nbsp; will turn into a non-breaking space. The server will never see the & (or the "nbsp;", for that matter).
  • SHRISH M
    SHRISH M almost 15 years
    Ok so the server will see a normal white space. That is also a bad thing :)
  • Sampson
    Sampson almost 15 years
    How does that differ from my answer?
  • Laurence Gonsalves
    Laurence Gonsalves almost 15 years
    You can have non-breaking spaces in filenames, at least in some filesystems. It's probably not a good idea, but saying "this entity has no meaning when used in a filesystem" is going a bit too far. It's just another character. (And if you're referring to the entity reference, &nbsp;, he was talking about using this in HTML, so the browser would take care of converting the entity reference to the corresponding character.)
  • Pete Kirkham
    Pete Kirkham almost 15 years
    The don't just represent spaces in different ways, the represent different spaces.
  • Laurence Gonsalves
    Laurence Gonsalves almost 15 years
    No, it'll see a non-breaking-space character. It's a different character from normal space.
  • Sampson
    Sampson almost 15 years
    Yes. I'm aware of that. But they still represent spaces - which is what I said.
  • Matthew
    Matthew almost 9 years
    Differences between these two types of space representations aside, do you know if html character entity references are permitted in URLs, either per the spec or in practical implementations?
  • Laurence Gonsalves
    Laurence Gonsalves almost 9 years
    @Matt Yes and no. HTML character entity references are permitted in URLs within HTML documents. In fact, ampersands in URLs in HTML documents are supposed to be HTML-escaped. Having "http://example.com/foo?bar=1&baz=2" in HTML is actually invalid. It should be "http://example.com/foo?bar=1&amp;baz=2". That said, the HTML entity reference isn't really "in the URL". It's in the HTML's representation of that URL. The URL itself just has & where the HTML contains &amp;.
  • Laurence Gonsalves
    Laurence Gonsalves almost 9 years
    @Matt Almost forgot: I believe ancient versions of MSIE would attempt to use HTML character entity references in form submissions for characters that could not be represented in the character set being used for the form submission. This was not standards compliant at all, which isn't too surprising for MSIE.
  • Matthew
    Matthew almost 9 years
    Really interesting. I thought this point was especially good: 'That said, the HTML entity reference isn't really "in the URL". It's in the HTML's representation of that URL.' Given what you said in the comments here, you might want to update your answer to distinguish between usage in URLs in the browser address bar vs. within HTML documents. The OP's question doesn't distinguish, but given your comments it is a critical difference.