Skip to content

Instantly share code, notes, and snippets.

@ivan-leschinsky
Forked from brentertz/rails
Last active August 29, 2015 14:07
Show Gist options
  • Save ivan-leschinsky/06cfa220603a8bdc0de8 to your computer and use it in GitHub Desktop.
Save ivan-leschinsky/06cfa220603a8bdc0de8 to your computer and use it in GitHub Desktop.
For SEO: Avoid SEO duplicate content issues
config.middleware.insert_before(0, 'Rack::SeoRedirect')
# Avoid SEO duplicate content issues
# - Remove www from domain
# - Remove trailing slash
module Rack
class SeoRedirect
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new env
url = request.url
url.sub!('//www.', '//') if request.host =~ /^www\./
url.chomp!('/') if request.path_info =~ %r{^/(.*)/$}
if url != request.url
[301, { 'Location' => url, 'Content-Type' => 'text/html' }, []]
else
@app.call env
end
end
end
end
require 'seo_redirect'
use Rack::SeoRedirect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment