Skip to content

Instantly share code, notes, and snippets.

@ivan-leschinsky
Forked from brentertz/rails
Last active August 29, 2015 14:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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