29

Is there consistency and/or a standard on how browsers send a url to a host related to trailing slashes?

Meaning, if I type in http://example.com in the address bar of a web browser, is the browser suppose to add a trailing slash (http://example.com/) or not?

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Hank
  • 1,151
  • 2
  • 10
  • 9

4 Answers4

32

The HTTP request sent from the browser to the server does not include the domain name, only the "path" portion (starting from the first slash after the domain name). Since the path cannot be empty, a / is sent in that case.

A sample GET request for the root of a web site might be:

GET / HTTP/1.0

The / above cannot be omitted.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    To clarify this, domain is sent in `Host` header (HTTP 1.1 only). – jholster Apr 05 '10 at 21:56
  • So what you're saying is that even if you have an empty path, the URL is guaranteed to have a trailing slash? – Hank Apr 05 '10 at 21:57
  • 3
    @Hank: What I'm saying is that *if* you have an empty path, a single `/` is sent. This doesn't say anything about trailing slashes if the path portion is *not* empty (in that case, the browser sends exactly what is on the URL bar, but the server may choose to redirect the browser in its response). – Greg Hewgill Apr 05 '10 at 21:58
  • 3
    (Sorry for intervention.) In other words: if the path is empty, browser adds /. If path is not empty, the trailing / is added by server (if added at all). – jholster Apr 05 '10 at 22:01
  • @jholster Wrong. See RFC 2616. A quote is here https://stackoverflow.com/a/2581514/529442 – Eugene Gr. Philippov Jan 14 '21 at 17:44
17

As RFC 2616 tells:

3.2.2 http URL

The "http" scheme is used to locate network resources via the HTTP
protocol. This section defines the scheme-specific syntax and
semantics for http URLs.

http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]

If the port is empty or not given, port 80 is assumed. The semantics
are that the identified resource is located at the server listening for TCP connections on that port of that host, and the Request-URI for the resource is abs_path (section 5.1.2). The use of IP addresses in URLs SHOULD be avoided whenever possible (see RFC 1900 [24]). If the abs_path is not present in the URL, it MUST be given as "/" when used as a Request-URI for a resource (section 5.1.2). If a proxy receives a host name which is not a fully qualified domain name, it MAY add its domain to the host name it received. If a proxy receives a fully qualified domain name, the proxy MUST NOT change the host name.

Read more: http://www.faqs.org/rfcs/rfc2616.html#ixzz0kGbpjYWa

5.1.2 Request-URI
...
For example, a client wishing to retrieve the resource above directly from the origin server would create a TCP connection to port 80 of the host "www.w3.org" and send the lines:

   GET /pub/WWW/TheProject.html HTTP/1.1
   Host: www.w3.org

followed by the remainder of the Request. Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root).

Read more: http://www.faqs.org/rfcs/rfc2616.html#ixzz0kGcaRbqU

Dor
  • 7,344
  • 4
  • 32
  • 45
10

Note that it's a very different matter when the URL has a path element:

http://example.com/dir

is a different URL than

http://example.com/dir/

and could in fact contain different content, and have a different search engine ranking.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • this is a good point. It's recommended to end the url with "/" – Claudio Redi Apr 05 '10 at 22:01
  • 3
    @Claudio, some reference please? – jholster Apr 05 '10 at 22:03
  • @Yaggo: take a look on google "trailing slash url". I don't remember a single place where I've read it, sorry man. – Claudio Redi Apr 05 '10 at 22:14
  • I did and found no evidence, only personal opinions. URIs without trailing slash are extremely popular (which alone doesn't quarantee anything) and I have never seen fact-based arguments against them. (Back when the web was mainly static files, appending / to directories was maybe de-facto standard, but I don't see a reason to prefer that nowdays.) – jholster Apr 05 '10 at 22:25
  • well, a url without "/" could be a file or a directory so the web server must do an extra check. If I'm not wrong some web server could by default do a redirect from www.whatever.com/dir to www.whatever.com/dir/. If I have to select an approach, trailing backslash would be the safer I think – Claudio Redi Apr 05 '10 at 22:58
  • 1
    The concept of 'directory' is vague in today's web. Leaving out trailing slash doesn't force server make "extra check" _per se_. URIs don't necessarily reflect a file hierarchy or have anything to do with files. I think your point of view is about statistic files, while I'm talking about web applications generally. – jholster Apr 05 '10 at 23:08
  • Yes, for rewritten URLs urls probably it's only question of prefference. I see your point. – Claudio Redi Apr 06 '10 at 01:29
0

As far as the protocol is concerned, http://example.com/something and http://example.com/something/ are quite different. Some servers might redirect you from one to the other if it is implemented in such a way.

As for the pure domain names, it always sends a request ending with a slash. (The domain name itself is not included in the path section of an HTTP request, just as Greg Hewgill and the others wrote. It is, however, included in the headers.)

You can check it with a tool like Fiddler or WireShark.

Venemo
  • 18,515
  • 13
  • 84
  • 125
  • This is incorrect. `/something/` is different from `/something` in an HTTP sense. http://googlewebmastercentral.blogspot.com.es/2010/04/to-slash-or-not-to-slash.html – Ryall Mar 04 '15 at 16:32