seo_urls plugin: Making 'show' pages more findable

Posted by bitbutter on July 25, 2007

Google favours pages when important keywords are reflected in their urls. Normally a page in a rails application that shows a single record has a url that looks like this recipes/show/12. If this particular recipe is for pasta sauce, Google would prefer the url to be recipes/show/12-pasta-sauce. Because rails isn't interested in what appears after the number in the :id part of the url, we are free to add words there to help search engines without affecting how our application functions.

The seo_urls plugin aims to make it easy to create more descriptive urls for 'show' pages and increase your application's visibility to search engines.

Install from the root of your application like so:

script/plugin install http://svn.redshiftmedia.com/svn/plugins/seo_urls

Apply the plugin to a model like this:

# Apply it to a model like this:
class Recipe < ActiveRecord::Base
   seo_urls
end

# Or to use an attribute other than name or title:
class Recipe < ActiveRecord::Base
   seo_urls "shortname"
end

The plugin looks for 'name' or 'title' attributes and if they are available it uses them to generate a search engine friendly string. to_param method is overwritten so that it returns the the numerical id of the record with the search engine friendly string appended to it.

# An example model
@recipe=>{:id=>12,:title=>"pasta sauce"}

# Without the plugin:
recipe_url(@recipe)=>"/recipes/show/12"

# With the plugin:
recipe_url(@recipe)=>"/recipes/show/12-pasta-sauce"
Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. David Fri, 27 Jul 2007 13:24:25 CDT

    Nice Plugin. But your example is unclear. You set the titel to "seo_urls rails plugin". So the post_url(@post) should return "/recipes/show/12-seo_urls_rails_plugin".

  2. bitbutter Sun, 29 Jul 2007 13:46:10 CDT

    Thanks for pointing that out David, I've fixed the example.

  3. David Sun, 12 Aug 2007 12:03:56 CDT

    This is brilliant!

    There are a number of solutions that allow you to do permalinks and SEO urls, but I stumbled accross one issue and that was integration with the methds ActiveResource creates for a model (like posts_url).

    This plugin completely solves that issue! And it doesn't use tables like the urlnamable plugin which means you have to have a 'urlnames' table which seems kinda unnecessary…

    Nice one Bitbutter.

Comments