3

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?

Andrew
  • 349
  • 1
  • 10
  • I don't have an answer, but I remember from somewhere that browsers won't cache a page if it has a trailing slash, as they consider it a 'directory'. Maybe there's no slash to help facilitate caching...? – nilamo Oct 23 '09 at 12:17
  • 8
    What makes you think it's against web standards? – harms Oct 23 '09 at 12:25
  • If people are going to downvote... please leave a comment as to why. – Jeremy Roberts Oct 23 '09 at 12:34
  • As for complete Rails routing documentation (not really relevant, but you might want to see how rails works), look here: http://guide.rails.info/routing.html –  Oct 23 '09 at 12:43
  • 1
    I don't think this should be downvoted. It's a reasonable question, if a little ill-informed. – John Topley Oct 23 '09 at 14:10
  • OK, I accept that it's not against Web standards. Thanks for the info. – Andrew Oct 24 '09 at 11:03

7 Answers7

12

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • :trailing_slash - If true, adds a trailing slash, as in “/archive/2005/”. Note that this is currently not recommended since it breaks caching. from http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_for. I'm not sure what they mean by that, they may be talking about rails caching. – jshen Oct 03 '11 at 18:59
5

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!

kikito
  • 51,734
  • 32
  • 149
  • 189
2

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
1

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.

JRL
  • 76,767
  • 18
  • 98
  • 146
0

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.

Community
  • 1
  • 1
carlosayam
  • 1,396
  • 1
  • 10
  • 15
0

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

kaleissin
  • 1,245
  • 13
  • 19
-3

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/
John Topley
  • 113,588
  • 46
  • 195
  • 237
GaryDevenay
  • 2,405
  • 2
  • 19
  • 41
  • I'm not sure that this really addresses the question at hand. It mentions it's "url re-writing" but then provides an example URI ending in a slash. The question was why are they missing trailing slashes sometimes. – coderjoe Oct 23 '09 at 15:12
  • the routing mechanism in Rails has nothing to do with URL rewriting. – carlosayam Sep 11 '12 at 07:25