The Mystery Of The Trailing Slash And The Relative url

I had heard conflicting rumors about the significance of the trailing slash, so I decided to go googling.  If you explore the first few hits you will find all sorts of discussions about cool URLs, the impact on SEO, the performance benefits of avoiding server redirects, amongst other stuff.  However, I found nothing that seemed to have a critical impact on the functionality of a web application.

My interest in the trailing slash had been provoked by my attempts to use relative URLs in some documents that I was returning from a REST server.  Sometimes the relative URLsjc would work sometimes they wouldn’t.

The performance benefit discussion that I had found related to the fact that web servers will automatically redirect to an URL with the trailing slash if the last segment points to a folder instead of a file. 

e.g. A request to would actually be redirected to (or whatever file is set as the default file)

As is probably obvious to anyone who has done any amount of web development, all relative URLs processed by a web server are relative to the folder in which the containing file is located.  Mechanically, this means the last segment is of the URL is stripped off and the relative URL is added.  A detailed explanation of how a relative URL is resolved can be found here.

The key point that I want to draw attention to is that the behavior is pretty natural when you are pulling static files from folders on a web server.  It gets less obvious when you start using dynamically generated resources with the style of URLs that are common in the REST world.

E.g.  Say you retrieve a resource such as and it contains an element <link href=” address”/>.  Common sense dictates that you are trying to get to, but that is not how it would be resolved.  The URL would actually be resolved as

The problem is that the server no longer has a file/folder distinction, so the routing infrastructure doesn’t know whether it should append a slash to your URL or not.

From what I’ve seen of Microsoft’s new URL routing infrastructure that is used by ASP.NET MVC and the WCF Rest toolkits, the URL gets passed through with or without the slash depending on what the user typed.  This is a problem if you want to use relative URLs in your representations.

I decided that I would include XML: base in documents so that at least I could be explicit about where my relative URLs were relative to.  This way I don’t care if the source URL ends with a slash or not.  I make sure that all my XML:base URLs end in a slash and so far this strategy is working for me.

Leave a Comment

Your email address will not be published.