SEO, Heroku, Ruby on Rails and removing those darn trailing slashes

One of the rules of the thumb for SEO is to make sure you don’t have any duplicate content. When you talk to SEO experts, they’ll say:

DO NOT HAVE ANY DUPLICATE CONTENT!

Phew, sorry for the yelling. It turns out that Google gets very confused when it finds two urls that have exactly the same content. For some reason, Mr. Google thinks these are two different pages:
http://www.movingtruck.com/moving-boxes/NJ/Bayonne
http://www.movingtruck.com/moving-boxes/NJ/Bayonne/

If you look in your webmaster tools account, you can see how you get penalized for this here:

Why? Who knows. The question is not to ask why, it’s how do we fix it?

So what does Heroku and Rails have to do with this? Heroku makes hosting Rails apps a breeze, it’s where I host all of my projects, including MovingTruck.com, which helps people find moving trucks (logical, huh?).  Unfortunately, Rails makes it a bit tough to remove trailing slashes, and Heroku is a bit of a locked down environment (in a good way, yeah Heroku!), so finding the answer to this vexing problem takes a bit of digging.

Enter my good friends at ELCTech, with this post.     Solving this is as simple as:

  1. running “sudo gem install rack-rewrite”
  2. and inserting the following code in your config/environment.rb file  (don’t forget to add the gem to your .gems file either)
config.gem 'rack-rewrite', '~> 0.1.2'

require 'rack-rewrite'
 config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
 r301 %r{^/(.*)/$}, '/$1'
end

And there you have it.  A tasty solution to your trailing slash SEO woes on Rails.  Enjoy.   Many thanks to ELC for their post.

 

UPDATE FOR RAILS 3

Here’s a quick update to get this working with Rails 3.

Instead of requiring the gem in your environment file, put the following line in you GemFile:

gem 'rack-rewrite', '~> 1.0.0'

And this in your config/application.rb

    config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
     r301 %r{^/(.*)/$}, '/$1'
    end

 

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

13 Responses to SEO, Heroku, Ruby on Rails and removing those darn trailing slashes

  1. Ignacio says:

    Thanks, it help me a lot to improve some little thinks on my heroku app.

  2. Jason Green says:

    Nice! A nice simple solution for heroku. Cheers

    • Buddy Beers says:

      Actually a followup comment…. It seems it’s slightly better SEO to redirect URLs without a slash to URLs with a trailing slash. It seems your regex magic could do just that as well, no? Unfortunately my regex skillz are noob at best. Would you mind posting a solution to do this as well or is it more complex than I imagine?

      • Doug says:

        Indeed, it is *slightly* better SEO wise. Unfortunately, my regex skills aren’t that great either. If anyone has mad regex skills and can figure it out, please comment.

  3. Rod says:

    The regex line you need is:

    r301 %r{^/(.*)/$}, ‘/$1’

    But wait, before you go ahead and implement it, note that it’ll break form posts! So unless you can limit the redirect to HTTP gets or something, it’s not an ideal solution.

  4. Pingback: SEO, Heroku, Ruby on Rails and removing those darn trailing slashes | Nance's Kitchen

  5. Pingback: SEO, Heroku, Ruby on Rails and removing those darn trailing slashes | Nance's Kitchen

  6. Christoffer says:

    Awesome, works like a charm! Thanks!

  7. Pingback: Ruby On Rails (2): Links, News and resources « Angel “Java” Lopez on Blog

  8. Pingback: Rack rewrite remove urls trailing slash, Heroku, Rails 3x, SEO | IT Support Newcastle

  9. My spouse and I stumbled over here from a different page and thought I might
    as well check things out. I like what I see so now i’m following you. Look forward to exploring your web page again.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s