W3C home > Mailing lists > Public > www-validator@w3.org > June 2008

W3C Markup Validation Service -- Ampersand (&) in URL

From: D. McKirahan <dmckirahan@comcast.net>
Date: Thu, 12 Jun 2008 08:58:11 -0500
Message-ID: <005901c8cc94$68045680$6401a8c0@hsd01.il.hsd1.il.comcast.net>
To: <www-validator@w3.org>
The following fails the W3C Markup Validation Service at http://validator.w3.org/ (Validate by Direct Input):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <a href="http://maps.google.com/local?output=html&q=Washington+DC">DC</a>

Checking "Clean up Markup with HTML Tidy" suggests that the offending line be changed to:
    <a href="http://maps.google.com/local?output=html&amp;q=Washington+DC">DC</a>

And, yes, this issue is discussed under Common Problems (see below).

However, the URL as written works and the HTML Tidy version does not -- 
when pasted into a browser's address bar.

I want my pages to validate so how do I resolve this issue?

Thank you.

--D. McKirahan


Ampersands (&'s) in URLs

Another common error occurs when including a URL which contains an ampersand ("&"):

<!-- This is invalid! --> <a href="foo.cgi?chapter=1&section=2&copy=3&lang=en">...</a>

This example generates an error for "unknown entity section" because the "&" is assumed to begin an entity reference. Browsers often recover safely from this kind of error, but real problems do occur in some cases. In this example, many browsers correctly convert &copy=3 to ©=3, which may cause the link to fail. Since &lang; is the HTML entity for the left-pointing angle bracket, some browsers also convert &lang=en to ?=en. And one old browser even finds the entity &sect;, converting &section=2 to §ion=2.

To avoid problems with both validators and browsers, always use &amp; in place of & when writing URLs in HTML:

<a href="foo.cgi?chapter=1&amp;section=2&amp;copy=3&amp;lang=en">....</a>

Note that replacing & with &amp; is only done when writing the URL in HTML, where "&" is a special character (along with "<" and ">"). When writing the same URL in a plain text email message or in the location bar of your browser, you would use "&" and not "&amp;". With HTML, the browser translates "&amp;" to "&" so the Web server would only see "&" and not "&amp;" in the query string of the request.
Received on Thursday, 12 June 2008 14:35:38 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 1 March 2016 14:17:56 UTC