Quick add slashes filter for Rails

Posted by Jeremy Voorhis Sun, 09 Oct 2005 18:25:03 GMT


def add_slash
  unless %r{/$}.match(@request.path)
    redirect_to @request.path << '/'
  end
end

This ensures you always have the trailing slash at the end of your URLs. Maybe it could be done better, but for now it works. Django has some stock middleware that does just this, as well as ensuring the first part of the hostname is ‘www’. The point is that your resource can be accessed with one and only one URL, rather than with potentially 4 combinations of / and www. I have no hard evidence, but this is supposedly good for teh Google, and some people find it elegant.

I am thinking about maybe whipping up a module of such utilities that you could include in your ApplicationController and never think about again.

Comments

Trackbacks

Use the following link to trackback from your own site:
http://www.jvoorhis.com/articles/trackback/37

(leave url/email »)