Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a trailing ampersand legal in a URL?

Tags:

A URL like

http://localhost/path?a=b&c=d 

is fine - but what is the status of the same URL with a trailing ampersand?

http://localhost/path?a=b&c=d& 

For example the Java Servlet API allows it where Scala's Spray does not (ie it throws an error).

I've tried to find the answer in the URI syntax spec, but not sure how to parse their grammar.

like image 433
Matthew Gilliard Avatar asked Jul 12 '11 11:07

Matthew Gilliard


People also ask

Can a website URL contain &?

Well, you have to use &amp; in a URL inside a link, e.g: <a href="example.com/x&amp;y">. You are correct in that case.

What does ampersand in URL mean?

In HTML, the ampersand character (“&”) declares the beginning of an entity reference (a special character). If you want one to appear in text on a web page you should use the encoded named entity “ &amp; ”—more technical mumbo-jumbo at w3c.org.


1 Answers

The URI syntax spec is for generic URIs. It allows anything in the query. I am not aware of any specification which actually specifies ampersand-separated key=value pairs. I believe it is merely convention. I know that PHP, for example, offers an option for using a different separator. But now, everyone uses ampersand-separated things when they want key-value pairs. You still occasionally come across things which use it for just a simple string, e.g. http://example.com/?example. This is perfectly valid.

The basic answer, though, is that & is valid anywhere in the query string, including at the end.


Demistifying the RFC syntax, or Why & is valid anywhere in the query string:

First, you have

query       = *( pchar / "/" / "?" ) 

(So a query string is made of any number of pchar and literal slashes and question marks.)

Going back, you have

pchar         = unreserved / pct-encoded / sub-delims / ":" / "@" 

And earlier still

sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"             / "*" / "+" / "," / ";" / "=" 

So a literal & is in sub-delims which is in pchar, so it's valid in query

like image 62
Chris Morgan Avatar answered Oct 21 '22 23:10

Chris Morgan