Wrox Home  
Professional Web 2.0 Programming
by Eric van der Vlist, Danny Ayers, Erik Bruchez, Joe Fawcett, Alessandro Vernet
November 2006, Paperback

Trailing Slashes and Location Independence

An issue that has generated some debate is the handling of the trailing slash: should you allow for /blog/archives/2006/08/07/ or /blog/archives/2006/08/07, or both? Consider the following rules of thumb:

  • If the last part of your path can itself be a container or a collection (that is contain other sub-resources) then terminate the resource with a /. For example, /blog/archives/2006/ could be the URI that displays a summary for all the months of year 2006, but the path can also be followed by a specific month number, so use a trailing slash.
  • If the last part of your path is a leaf resource, like an individual article or post, which cannot contain sub-resources, then omit the trailing slash. For example: /blog/archives/2006/08/07/Web20-thebook.
  • Avoid using a path with a trailing slash and one without to point to two different resources.

If you opt for this strategy, you can be even more user-friendly by redirecting URIs with missing trailing slashes to URIs with slashes. For example, redirect /blog/archives/2006/08/07 to /blog/archives/2006/08/07/ with a permanent redirect.

The use of a trailing slash to signify the root of a particular collection enables you to get rid of URIs that end with index.html or default.html,and the like: just end your URI with a / instead. There is no need to externally expose the name of index or default pages, as their names too can change.

You must choose whether you use absolute URIs, absolute paths, or relative paths when using URI references (such as hyperlinks, reference to images, and so on) in the documents you serve. Consider the XHTML page served by http://example.org/author/clarke. You want to display an image of the author within the page. You can refer to it with an absolute path from XHTML:

<img src="/author/clarke/portrait.jpg"/>

Using such an absolute path has the drawback that the resource cannot be moved around on a server without also changing all of the paths it uses. For this reason, many HTML authors use relative paths as much as possible, especially when resources can be grouped (as in this case of an information page in XHTML with an accompanying image). But what relative URI do you use? Keep in mind that relative paths are usually resolved by the client using the URI that requested the resource as base URI.

If the location of the image is http://example.org/author/clarke/portrait.jpg, the shortest relative path is:

<img src="clarke/portrait.jpg"/>

This has the drawback that the XHTML page must know at least part of its own location, clarke.

Now if the original page is served by http://example.org/author/clarke/ (note the trailing slash) the shortest relative path becomes:

<img src="portrait.jpg"/>

This is optimal from the perspective of shortness of URI and location independence. On the other hand, it requires that the resource be loaded from a URI with a trailing slash.

On the other hand, if you opt for serving image resources from a separate hierarchy, for example, with /images/clarke.jpg, using absolute paths becomes a good solution again. The bottom line is that there is no single perfect solution and that the final choice is yours!

Excerpted from Chapter 16, "Implementing and Maintaining Your URI Space," by Erik Bruchez from Professional Web 2.0 Programming, by Eric van der Vlist, Danny Ayers, Erik Bruchez, Joe Fawcett, Alessandro Vernet (Wrox, 2006, ISBN: 0-470-08788-9). Erik Bruchez has extensive experience in the software industry as a software architect and consultant. As a former employee of Symantec Corporation, he contributed to the VisualCafe for Java product line. In 1999, he co-founded Orbeon, Inc. (www.orbeon.com), where he is now an architect of Orbeon PresentationServer (OPS), an open source Web platform for form-based applications that builds on technologies, such as XForms and Ajax. Erik participates in the W3C's XForms and XML Processing Model working groups. He is the author of articles about Web applications and XML technologies and has been a speaker at conferences such as JavaOne, ObjectWebCon, and XTech. Erik holds an MS/CS degree from the Swiss Institute of Technology (EPFL) in Lausanne, Switzerland.