12

What is the difference between these two URLs:

  • http://example.com/
  • http://example.com

Should we always add the final / or avoid it? Does it make a difference?

dan
  • 15,123
  • 11
  • 44
  • 52
Luc M
  • 285
  • 1
  • 4
  • 11

4 Answers4

13

There's no difference between them. (As opposed to not putting a slash on links into a directory, for example.) I don't think I've ever seen anything saying that you should or shouldn't use a trailing slash for absolute URLs, though being consistent in your own behavior is generally not a bad idea either.

Su'
  • 19,332
  • 3
  • 41
  • 65
  • The server internally adds it for the request, at one time you got quicker response by having it there ahead of time. Fast hardware now, meh. – Fiasco Labs Sep 03 '14 at 14:49
  • @FiascoLabs: actually, per the HTTP spec adding the trailing slash for the root domain should be performed by the client, not the server. In other words, your browser usually adds its before requesting the page from the server. – MestreLion May 27 '23 at 08:41
6

One of the most wasteful redirects happens frequently and web developers are generally not aware of it. It occurs when a trailing slash (/) is missing from a URL that should otherwise have one. For example, going to "http://astrology.yahoo.com/astrology" results in a 301 response containing a redirect to "http://astrology.yahoo.com/astrology/" (notice the added trailing slash). This is fixed in Apache by using Alias or mod_rewrite, or the DirectorySlash directive if you're using Apache handlers.

from: http://developer.yahoo.com/performance/rules.html

Adam
  • 209
  • 2
  • 6
  • 5
    But this does not apply to the question example – leonbloy Oct 03 '11 at 02:25
  • @leonbloy how does it not? as it shows not only a programmer issue, but given the site you frequent, could mean milliseconds of difference in page load time! – SpYk3HH Sep 02 '14 at 21:41
  • @SpYk3HH My comment (almost 3 years ago) referred to a previous version of the question, the posterior editions changed (I wonder why) the original meaning. http://webmasters.stackexchange.com/posts/20373/revisions – leonbloy Sep 03 '14 at 00:30
  • 2
    @leonbloy Unfortunately the edits did change the meaning significantly, so I rolled the question back to an earlier version. – dan Sep 03 '14 at 11:18
  • 1
    @dan Well done, IMO. The accepted answer makes the proper distinction. – leonbloy Sep 03 '14 at 11:49
2

The other answers have identified that it doesn't seem to technically matter.

For me it is a matter of perception, if there is a trailing slash I'd expect something to be following it, a file name, an anchor.

I also think a lack of a trailing slash looks cleaner.

MrG
  • 1,782
  • 11
  • 17
-1

It's a matter of Apache configuration (can't say about others)

Some Apaches can't handle site/path as site/path/index.file

Semantically both URL declare the same resource

Lazy Badger
  • 1,347
  • 7
  • 12
  • It's nothing whatsoever to do with Apache. It's a browser issue. – DisgruntledGoat Oct 03 '11 at 14:30
  • @disgruntledgoat- Wrong statement! The DirectorySlash directive determines whether mod_dir should fixup URLs pointing to a directory or not.

    Typically if a user requests a resource without a trailing slash, which points to a directory, mod_dir redirects him to the same resource, but with trailing slash for some good reasons...

    – Lazy Badger Oct 03 '11 at 14:38
  • 2
    the question is not about directories, it's about the root domain. – DisgruntledGoat Oct 03 '11 at 15:01
  • 1
    Apache is not the only webserver – TheTechGuy Jan 25 '12 at 18:47