8

Maybe too basic of a question but wanted to see if there is more to it than I am thinking.

When configuring URLs (baseUrls, like https://my.app or relative URLs like /path/to/resource) either as a static variable, in configuration files or environment variables etc., what should my 'slash strategy' be?

One of my coworkers mentioned that I should always configure base URLs with a trailing slash (https://my.app/) and paths with a leading and trailing slash (/path/to/resource/) because in most cases when you concatenate these, you may end up with double slashes (http://my.app//path/to/resource/) but most tools can handle that. If the claim that most tools can handle it is true, this could be an attractive approach since all you need to know is to always use slashes everywhere.

The other solutions could either be to follow and be consistent with either one of these approaches:

  • No trailing slashes but always use leading ones (http://my.app, /path/to/resource)
  • No leading slashes, but always use trailing ones (http://my.app/, path/to/resource/)

Is there a best strategy? Would the strategies be dependent on the tools being used (like libraries that can handle one but not the other?)

c_maker
  • 8,250
  • 4
  • 35
  • 53
  • 1
    See also: http://stackoverflow.com/questions/5948659/when-should-i-use-a-trailing-slash-in-my-url – Eric King Mar 25 '17 at 20:35
  • 7
    What you're looking at is a server-specific behavior. `http://foo/bar` and `http://foo/bar/` are technically two different locations. The important thing is to pick one and be consistent. – Blrfl Mar 25 '17 at 21:16
  • 1
    For what it's worth, I encountered a bug once where a path was calculated as `basePath + "/api/info"`, but `basePath` was `"/"`, resulting in the application trying to use the path `"//api/info"` (server `api`, path `/info`) instead of `"/api/info"` (current server, path `/api/info`). I fixed it by changing `basePath` to `"/."`. – Tanner Swett Mar 26 '17 at 04:45
  • I don't know if It may help you but when formatting API web URIs the trailing slash should be avoided due to has no semantic value and it may cause confusion. After a while working on API webs I have found to be easier to work with your first option. – Laiv Mar 26 '17 at 18:34
  • 1
    Working without leading slashes (relative paths) IMO has the recipe for all sort of surpises. Finally I find that format less readable. – Laiv Mar 26 '17 at 18:37

2 Answers2

12

Do not use bare strings to record URIs. Most languages have a type for them that handles joining absolute and relative paths automatically, ensuring you never need to sorry about doubled (or missing) slashes. Using this type will save you no end of problems,

Jules
  • 17,614
  • 2
  • 33
  • 63
9

Always including a trailing slash on the base URL and omitting the leading slash on the relative URL would allow you to concatenate the two in a manner that matches the spec on combining a base URL with relative URL to derive a full URL (RFC1808, Section 4).

Doing it this way would allow you to have a consistent method that is also the least surprising.

Josh Coady
  • 363
  • 1
  • 6