QR Code contains TinyURL of this article.That Trailing Slash Issue

What’s the difference between the following two URI‍s?

  • /blog/example/
  • /blog/example

The first, with the trailing slash, references a directory or collection of files. The second references a specific file. Yet it is common to find websites where URI‍s with trailing slashes address pages and directories. This behaviour is incorrect. Some websites render pages irrespective of a trailing slash, reinforcing the illusion that it is of no consequence. Again, this is incorrect.

Why do webmasters get this wrong? I think a lot of the confusion stems from the fact that we all try to use clean URL‍s where possible. Such URL‍s often omit the file extension of a resource via URL rewriting, content negotiation or, most commonly, because they are referencing a record in a database rather than an actual file.

If we put the assumed file extension in place then the error of this trailing slash practise becomes immediately apparent:

  • /blog/example.html/
  • /blog/example.html

The first URI now looks wrong doesn’t it? As we can see, a “page” address shouldn’t have a trailing slash, only a “directory” should.

Directly Addressing the File System

Since I am using a file-system based CMS for this site, my URL‍s have to be correct as they are directly addressing physical files on the server’s hard disk, inside a real directory structure. URI‍s here have a trailing slash when addressing collections of pages and no trailing slash for the pages themselves.

Note: I made a design decision to use a combination of URL rewriting and content negotiation for future proofing my URI‍s by omitting file extensions. I’ve done this because, even though I might make changes to the underlying architecture of this site, “Cool URI‍s Don’t Change.”

See Also:

Updated: 2nd October, 2012.

I forgot to write above that there is one situation where you would legitimately have a page URL that ends with a trailing slash: and that is when a page is the default file in a directory. These are usually named something like index.htm or index.php and, if they exist, they will be automatically rendered upon a request to the parent directory.

Updated: 3rd October, 2012.

How embarrassing it was to publish this page, telling the whole world how to properly form URI‍s and boasting about how I’d done it right, only to get an email from a reader directing me to a couple of links on this website that were addressing pages with trailing slashes! Oh, the humility, the shame.

In my defence, I had configured the canonical URL‍s correctly. I’d just been slack in creating my internal links. I have now corrected these. After all, we should eat our own dog-food shouldn’t we?