One of the most important components of a website is URL design. I have obsessed over URLs since the beginning of time, and REST caused me to focus on them even more. Here's a classic example of a clean URL:
www.strictlyuntyped.com/articles/1/comments/1This seems quite ordinary by today's standards. But admit it, a few years ago us web developers were getting really excited about this stuff.
What was the original problem?
Let's start from the beginning. The 'dirty' version of the above URL:
www.strictlyuntyped.com/read?article_id=1&comment_id=1One of the most notable problems with this type of URL is that the user sees it. The URL is an important piece of the user experience, since (1) it is displayed at the top of the browser window, (2) users determine where they are on a site by the URL, and (3) they type in and share URLs, so they must be 'readable'. Essentially, no information is provided to the user about the structure and/or hierarchy of your site.
The second problem with dirty URLs are developer facing. The application code pretty much consists of a function that takes in a hash. A Ruby analogy would be the following method:
def read(options={}) |
The method has zero self-documentation, and a comment would be required to determine what options it expects. Our world here is essentially a one dimensional array of methods that take a hash.
As a comparison, here is what I believe the Rails analogy of the aforementioned clean URL would be:
Article.find(article_id).comments.find(comment_id) |
The idea of introducing structure into a line of characters is a simple, yet profound, concept. It seems to have become a requirement for any 'Web 2.0' site.
Don't go overboard with clean RESTful URLs
Clean URLs and REST works incredibly well for content managing systems such as blogs. Without coincidence, Rails works very well with these types of systems. In other cases, I see myself, and others, forcing a square through a round hole when it comes to URL design. Here are two mistakes that myself, and probably others, have made.
Mistake #1: Overcomplicated Hierarchies
There exists an implicit hierarchy to clean URL design; an article having comments is a good example. On the other hand, there is no reason to create deeply nested hierarchies for the sake of representing the real world. the { buckblogs :here } says not to nest resources more than one level deep from a programmatic point of view. I completely agree, and amend his reasons from an information design perspective. If you were to make a website about animals, would the urls represent biological classifications? Here is what the URL for a lion will look like:
/kingdom/animalia/phylum/chordata/class/mammalia/order/carnivora/family/felidae/genus/panthera/species/lionThe point is, flatten your hierarchy and make Tufte proud!
Mistake #2: Unordered Parameters
Oftentimes, the input required for a URL does not fit in a hierarchy. A great analogy is a Ruby method. Think of how many methods in Ruby on Rails use the following format:
def some_method(foo, bar, options = {}) |
This method makes the options optional, and they can be passed in without any specific order. Why not leverage these same benefits with a URL? With that in mind, there is nothing wrong with the following URL:
www.strictlyuntyped.com/articles/1/comments?order=date&rating=4How else would Google Maps make locations sharable?
To wrap up my ramblings, don't get stuck with your project obsessing over how perfect your URLs are. Keep the hierarchy simple, and use a hash of parameters when you need it.