3 ms·
Trailing slashes should be optional and the server response should be the same regardless of their presence. At least, that's how I expect my applications to be
by jillesvangurp 5y ago
Trailing slashes should be optional and the server response should be the same regardless of their presence. At least, that's how I expect my applications to behave. It's the principle of the least amount of surprise. You might be pleasantly surprised that it works if you add the slash as opposed to be mildly annoyed when it gives you a 404. Requiring the slash would be very surprising. The least surprising is that they both work the same way. I see no technical reason for them to behave differently.
Some web-servers do this and others require fiddling with to get them to behave that way. But ultimately. the slash is just a separator and not semantically relevant. It's like many languages now allowing trailing commas in lists. It's convenient. The comma does not add an extra element.
One place where this comes up in practice is with specifying base URIs in e.g. configuration files. Somewhere else, this base URI is consumed to construct a full URI using a suffix that may or may not have a leading slash.
If your base URL is http://foo(/) http://foo(/) and you want to append (/)bar, you might end up with http://foobar http://foobar, http://foo//bar http://foo//bar, http://foo/bar http://foo/bar depending on what people do on both sides. Or those three with a trailing slash. There is no right answer.
The only sane behavior that follows the principle of the least amount of surprise is to make sure that base uri and suffix will be separated by exactly one slash and assume nothing about the presence of leading or trailing slashes. That way, nothing will break or behave unexpectedly if people add or omit a trailing or leading slash.