SEO friendly title tag plugin

written by Eadz on June 15th, 2008 @ 05:45 AM

I’ve come across a small, but very useful plugin for Ruby on Rails applications. It was designed with SEO in mind, and it makes keeping your meta title tags, and your <h1> tags in sync easy.

The plugin is hosted a (the very awesome) GitHub : http://github.com/DefV/title_helper/tree/master

Here’s how it works.

In your application layout you call the plugin, passing in your overall site title.


<title><%= title :site_name => "The SEO on Rails Blog" %></title>

If you do nothing else, all pages on your site will have the title “Seo on Rails”.

In your view templates, say app/views/blogpost/view.html.erb you could have this:

<%= title "SEO plugins for Ruby on Rails" %>
This will print out a H1 tag:

<h1>SEO plugins for Ruby on Rails</h1>
At the same time, the plugin will remember the title and insert it into the layout’s <title> tag before your :site_name. In our example that would result in :

<tiitle>SEO plugins for Ruby on Rails - The SEO on Rails Blog</title>

Keeping your <H1> tags and your <title> tags in sync is a good thing, and this plugin helps make it very easy.

Screen scraping update ( Already! )

written by Eadz on February 8th, 2007 @ 12:37 AM

Wow the ruby/rails community works fast. Igvita has an article explaining how to use hpricot with the magic of firefox’s firebug pluign. And scRUBYt has been released, which is a billed as WWW::Mechanize and Hpricot on Steroids

301 Redirects in Rails

written by Eadz on February 5th, 2007 @ 09:41 AM

A 301 redirect is a permanent redirect, which tells the search engines to index the redirected to URL rather than the current URL.

You can use this when migrating from legacy content management systems, or to make sure that you only have 1 URL per page.

With the folloing code in your controller, Rails makes it easy :

1
2
      headers["Status"] = "301 Moved Permanently"
      redirect_to "http://someurl/"

RoR image_tag harmful for SEO?

written by Eadz on February 3rd, 2007 @ 09:09 AM

We all know image_tag, but is it harmful for SEO? By default, it will add an alt tag consisting of the filename, without the extension, and capitalised.

How could this be bad? Well, the question comes down to : is an irelevent alt tag worse than a blank one?

The code in question is one line of the image_tag method in asset_tag_helper.rb.


  options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize

To answer this question, it depends. If your image names are not related to your content then it could be.

To be safe, always pass :alt => with your image_tag.

Update: I asked Matt Cutts his thoughts about this and here is the reply

If you have a picture of a monkey with filename like apple.jpg or dsc00001.jpg, you shouldn’t have any issues if the alt title is “apple” or “dsc00001”. However, if the image filename is cheap-viagra-debt-mortgages.jpg, then you could run into problems for sure. I’d look at image filenames to make sure that they couldn’t be viewed as keyword-stuffing or spammy. And it doesn’t hurt to preserve the extension (.jpg) in the alt title just so that people realize that the alt title comes from the image filename.

Getting your meta tags on track

written by Eadz on February 3rd, 2007 @ 06:17 AM

Sick of the lame Rails puns in the headlines yet? Great!

Ok, so this post is a kind of ‘Ask the community’ post. How do you deal with meta tags including page titles? We should all know the ideal for titles is to be unique for each page on the site , but how do you go about this the rails way?

Here’s what I do (and it’s just one way of many) :

In my layout/application.rhtml
1
2
3
<title><%= @meta_title %> My Site Name</title>
<meta name="keywords" content="<%= @meta_keywords %>" />
<meta name="description" content="<%= @meta_description %>" />

In my application controller:

1
2
3
4
5
6
7
8
  before_filter :meta_defaults
  private

 def meta_defaults
    @meta_title = "Welcome to"
    @meta_keywords = "my keywords"
    @meta_description = "my meta description"
  end
and then in individual actions in my controllers I override the defaults
1
2
3
4
5
def view
    @article = Article.find(params[:id])
    @meta_title = "#{@article.name} - "
    @meta_description = @article.short_description
end

I’ve also seen suggestions for using yield and content_for, but to me that is a bit heavy-weight for simple strings. So, what do you do?

Screen Scraping in Ruby

written by Eadz on February 1st, 2007 @ 03:45 AM

I’m not going to make a full post on this because over on Rubyrailways.com they have an excellent post covering the main libraries for screen scraping in ruby.

However it is a bit out of date being 7 months old, and since then 2 more excellent libraries have emerged.

Hpricot from the Ruby Superstar _why which has just been updated to version 0.5 today!

And Scrapi which is also excellent.

Google Sitemaps in Mephisto

written by Eadz on February 1st, 2007 @ 03:24 AM

Well we call them Google Sitemaps, but MSN and Yahoo are also on board and have formed sitemaps.org. So I guess we just call them XML sitemaps?

The 3 search engines currently don’t support the full sitemaps protocol, which is shame, hopefully by later this year they will.

First off : Mephisto, the Ruby on Rails blogging software that powers this site.

Creating a sitemap is no trouble at all with the Mephisto sitemap plugin. Although to get it working on this blog, I had to change the following in vendor/plugins/mephisto_google_sitemap/lib/mephisto_google_site_map.rb from:

1
2
3
4
5
    def lastmod(article)
      time_zone = TimeZone.new(article.site.timezone.current_period.utc_offset)
      latest_comment = article.comments_count.zero? ? nil : article.comments.calculate(:max, :updated_at)
      (latest_comment.nil? ? article.updated_at : latest_comment).strftime("%Y-%m-%dT%H:%M:%S#{time_zone.formatted_offset}")
    end

to:

1
2
3
4
    def lastmod(article)
      latest_comment = article.comments_count.zero? ? nil : article.comments.calculate(:max, :updated_at)
      (latest_comment.nil? ? article.updated_at : latest_comment).strftime("%Y-%m-%dT%H:%M:%S+00:00")
    end

Change +00:00 to what ever your timezone offset from UTC is. Hopefully this will be fixed in a later version of the sitemap plugin.

URLs ending in .html

written by Eadz on January 24th, 2007 @ 08:16 PM

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'

Even better looking URLs with permalink_fu

written by Eadz on January 24th, 2007 @ 11:08 AM

Extracted from Mephisto is the handy plugin called permalink_fu. This plugin allows you to use ID-less permalinks, much like that in this blog.

I wrote earlier about how to use to_param for nicer URLs, but these are even better. The plugin automatically turns a field in your model into something that could be a permalink, by stripping out all non english and non alphanumeric letters. It then saves this in a field in your database for future use.

So, onto the HowTo:

First, install the plugin in your rails app.


./script/plugin install http://svn.techno-weenie.net/projects/plugins/permalink_fu/

Now you need to add a field to your database to store the permalink.


./script/generate migration add_permalink_to_article

In the migration we add the field called permalink.


add_column :designers, :permalink, :string
Now for the model code
1
2
3
4
5
6
7
8
9
10
11
class Article < ActiveRecord::Base
        # title is the field name you want to convert to a permalink
        has_permalink :title 
        # you can also specifiy a different permalink field in your database by giving a second paramater
        # has_permalink :title, :my_permalink_field
       
        # we now add the to_param method which Rails's routing uses
        def to_param
              permalink
        end         
end

The permalink field is only populated after the record is validated, so if you have a bunch of records already in your database you can get the permalink field to populate with


Article.find(:all).each(&:save)
If you already have lots of view code written then this is the easy way to get these routes working – in your controller :

@article = Article.find_by_permalink(params[:id])

However that doesn’t seem to pass the smell test using :id rather than :permalink, but will work fine. To use :permalink you will have to change your routing code and your view code :

1
2
3
4
5
6
# in your route file
map.connect 'article/:permalink', :controller => 'article', :action => 'view'
# in your views when linking
link_to "View #{article.title}", {:controller => 'designer', :action => 'view', :permalink => article.permalink}
# then in your controller you can use
@article = Article.find_by_permalink(params[:permalink])

Thats all! I’m always on the lookout for SEO related Rails topics to talk about so drop me a line @ questions at this domain .com.

Better looking URLs with to_param

written by Eadz on January 24th, 2007 @ 02:32 AM

Nice URLs aren’t just for search engines, but for us mortals too. By default rails URLs look like /controller/action/1. Rails has built in support for URLs that look like /controller/action/1-my-article. This is achieved by implementing to_param in your model.

This code will turn an Article with ID 22 and title ‘Nice URLs’ into 22-Nice-URLs
1
2
3
4
5
class Article < ActiveRecord::Base
        def to_param
          "#{id}-#{title}"
        end 
end

These URLs will work automatically, providing you have the ID in the first part of the URL. This works because Ruby will convert ‘123-hello-world’ into 123 when to_i is called on the string :


>> "123-hello-world-1".to_i
=> 123

However you may have some funny characters in your title so you want to strip them out and convert them to hyphens. You might also want to make your urls lower case. This code does just that.

1
2
3
4
5
class Article < ActiveRecord::Base
        def to_param
          "#{id}-#{title.downcase.gsub(/[^[:alnum:]]/,'-')}".gsub(/-{2,}/,'-')
        end 
end

Some explanation is required; .downcase converts the title to lower case, the 1st .gsub strips out anything not alphanumeric and turns it into a hyphen, and the 2nd .gsub changes any multiple hyphens into a single hyphen.

It is possible to have URLs without the ID in them, but you will have to work out what model object you need yourself in the controller. I hope to cover this in a later blog post.

Welcome to SEO on Rails

written by Eadz on January 23rd, 2007 @ 10:46 AM

First Post!

Welcome to SEO on Rails. I hope to bring you lots of tips, code and ideas to help your Ruby on Rails powered site more search engine friendly and optimized.

If you have any rails related SEO questions, please email me – questions @ this domain.

Options:

Size

Colors