10

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?)

5
  • 1
    See also: stackoverflow.com/questions/5948659/…
    – Eric King
    Commented Mar 25, 2017 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
    Commented Mar 25, 2017 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 "/.". Commented Mar 26, 2017 at 4: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
    Commented Mar 26, 2017 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
    Commented Mar 26, 2017 at 18:37

2 Answers 2

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,

1
  • 1
    I agree with this, use libraries to deal with URLs and file system paths.
    – Matthew
    Commented Mar 30, 2017 at 17:14
10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.