views:

223

answers:

2

Is it safe to always skip the trailing slash when appending a query string?

http://example.com?querystring

Instead of...

http://example.com/?querystring

All webhosts I've used support this but Is it safe to assume that all server environments will support this method, is it standard?

+11  A: 

No. It is not okay to skip the slash. It may work in a modern browser. However, that does not make it correct.

See RFC1738 - URL and RFC2396 - URI.

The format per RFC1738 (I have excluded the schema format here):

//<user>:<password>@<host>:<port>/<url-path>

And it goes on to note that: """...the "/" between the host (or port) and the url-path is NOT part of the url-path.""".

In this case the "?" is part of the url-path which """...depends on the scheme being used, as does the manner in which it is interpreted."""

Also note that, per specification, it is perfectly valid to omit "/url-path" -- note that the "/" has been explicit included in this case.

Thus, foo.com?bar is invalid because there is no "/" before the url-path.

pst
Quoting RFCs ignores the simple fact that most web servers support this without issue. You're talking textbook versus reality again.
Nissan Fan
+1  A: 

It is not safe to assume that. Web servers and self-contained web applications typically inspect the URL provided in the request, but there is no guarantee that they will treat /abc equal to /abc/. Web servers and self-contained web applications can do whatever they like with the information gleaned from the URL, and it will not necessarily be what you expect. You will have to find out what the convention is for the particular URL in question.

Note, of course, that most web servers and web application frameworks try hard to accept all sorts of inputs and deal with them appropriately. Therefore, in most cases, the web server or self-contained web application will treat /abc equal to /abc/. But remember, because the server can do whatever it likes with the path, that this is simply a generic observation with potentially numerous exceptions.

Justice