Question

Why do so many Ruby on Rails apps have missing trailing slashes in their URLs? One example is http://basecamphq.com/tour. AFAIK this goes against Web standards. Is it something to do with the way RoR is set up?

Was it helpful?

Solution

It's not against Web standards. http://basecamphq.com/tour is considered a file, http://basecamphq.com/tour/ would be a directory (Note: both URLs aren't equal, although some webservers - e.g. Apache - will check the other if one doesn't exist). As both are kind of virtual, it's mainly up to the developer to decide (this is independent of used programming languages or frameworks).

I don't think it has something to do with caching (as mentioned by nilamo) as there are enough HTTP headers for cache control - might be that some reverse proxies have different default behavior though.

OTHER TIPS

Your argument is invalid:

w3c's url spec doesn't enforce trailing slashes on urls.

This is what it says about slashes:

The path is interpreted in a manner dependent on the scheme being used. Generally, the reserved slash "/" character (ASCII 2F hex) denotes a level in a hierarchical structure, the higher level part to the left of the slash.

Rails adheres quite well to this directive.

My hair is a bird!

Because trailing slash denotes a directory, and you are not accessing directories in Rails, but pages. It's like tour.html in your example, except that .html can be ignored as it is the default.

I'd venture to say that since in RoR, the URL you type usually does not map to a static file in a directory, but is resolved dynamically by the routes.rb file, ending the path with a trailing slash doesn't make much sense.

Some like slashes, some don't. Impassioned arguments can be made for both sides.

Rails uses slashes as parameter token separators, and a route like

/post/:year/:page

matches by default both, /post/2012/a-title and /post/2012/a-title/, unless you do some magic. This has nothing to do with web standards.

From the point of view of the browser, these two paths are very different when it comes to deal with relative resources. In a response to the above with <img src="image.png"/> the browser will send a second query to the server for: /post/2012/image.png (first case) or /post/2012/a-title/image.png (second case), because the browser uses the trailing slash to resolve paths as if they were directories.

However, Rails developers usually don't care because they don't write URLs explicitly when rendering content! They have at their disposal URL helpers which hide this logic from them... unless you don't use the helpers to generate content, then you care.

This is a form of URL Re-writing. It is not against web standard and actually does a lot for usability and has been proven to help your search engine rankings. Think of it this way.

You are telling your friend about this cool post you seen on someone's blog. Which URL is easier to tell your friend:

  1. http://www.coolwebsite.com/post.aspx?id=aebe6ca7-6c65-4b5c-bac8-9849faa0a467

OR

  1. http://www.coolwebsite.com/cool-ideas-for-posts/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top