Skip to content

Instantly share code, notes, and snippets.

@cjolly
Created October 15, 2011 18:22
Show Gist options
  • Save cjolly/1289924 to your computer and use it in GitHub Desktop.
Save cjolly/1289924 to your computer and use it in GitHub Desktop.
SEO on Rails or Rack
# As a general rule, I despise all things SEO with it's witchery and voodoo ways.
# However, these two practices are confirmed semi-non-witchcraft SEO best practice.
#
# 1. Serve your content from a consistent domain (www vs. non-www, Doesn't matter. Just be consistent)
# In addition this is nice for Heroku and other cloud based deployment environments that leave your app
# accessible from a url like http://myapp.herokuapp.com
#
# 2. Believe it or not, trailing slashes matter. The google machine interprets http://domain.com/about
# as a different page than domain.com/about/
# (source: http://googlewebmastercentral.blogspot.com/2010/04/to-slash-or-not-to-slash.html )
# https://github.com/jtrupiano/rack-rewrite
# Gemfile
gem "rack-rewrite"
# config/environments/production.rb
config.middleware.use Rack::Rewrite do
# SEO Rewrites
# - Consistent domain
r301 %r{.*}, "http://my-domain.com$&",
:if => Proc.new {|rack_env| rack_env['SERVER_NAME'] != "my-domain.com" }
# - Remove trailing slash
r301 %r{^(.+)/$}, '$1'
end
@cjolly
Copy link
Author

cjolly commented Oct 15, 2011

Does anyone have an opinion on the best place in the middleware stack for rewrite rules? I'm thinking it could be the first middleware before Rack::Lock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment