URLs ending in .html
This is just a personal preference of mine, but I like to have URLs end in .html. Fortunately rails makes this easy:
In your routes file:
map.connect ':controller/:action/:id.html', :format => 'html' |
For your other custom routes you will need to also add :format => ‘html’ and the .html at the end of the route line like above.
Update
Another (better?) way to achieve nice URLs in rails, with .html suffixes is to add the following to your ApplicationController:1 2 3 |
def default_url_options(options) { :format => 'html' } end |
This means your routing line is much cleaner. In fact it’s the default!
map.connect ':controller/:action/:id.:format' |
Like this article? Subscribe to the SEO on Rails feed.
Comments
-
Cool tip. I've used something like this recently to move a static site over to a Rails driven one, whilst still keeping all the same URLs. With that route, I think you mean the following though:
The only thing that might concern me is if Google assumes that a 'html' page might update less than one otherwise... but that would be pretty silly assumption, and now I've said that, I don't think that'd be the case.map.connect ':controller/:action/:id.:format', :format => 'html' -
Hey Tim, thanks for the comment. I *wish* that route worked, it would be a lot nicer. However when using link_to without :format => 'html' then the urls end up like '/controler/action/id.' Rails treats 'html' as the default format, yet fails to populate a route with :format in it unless :format is explicitly specified in link_to. I'm not quite at the stage of rails core hacking, but I know you are :)
-
Ahh yes, I spose it would ditch it in that case... hrmm bugger. Spose you could add :format => 'html' to default_url_options or something, but sounds like a hack.
-
Ahh. Thanks for pointing out default_url_options. I don't think it's a hack.. especially compared to a routing line with .html in it! Actually, default_url_options works really well:
In ApplicationController:
def default_url_options(options) { :format => 'html' } endThis means your routing line is much cleaner. In fact it's the default!
map.connect ':controller/:action/:id.:format'
Awesome, thanks Tim.
