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:
- running “sudo gem install rack-rewrite”
- 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
Thanks, it help me a lot to improve some little thinks on my heroku app.
Nice! A nice simple solution for heroku. Cheers
Killer. Thanks.
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?
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.
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.
Bad WordPress, bad. Those are supposed to be single quotes around the /$1.
Pingback: SEO, Heroku, Ruby on Rails and removing those darn trailing slashes | Nance's Kitchen
Pingback: SEO, Heroku, Ruby on Rails and removing those darn trailing slashes | Nance's Kitchen
Awesome, works like a charm! Thanks!
Pingback: Ruby On Rails (2): Links, News and resources « Angel “Java” Lopez on Blog
Pingback: Rack rewrite remove urls trailing slash, Heroku, Rails 3x, SEO | IT Support Newcastle
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.