Skip to content

Instantly share code, notes, and snippets.

@kinopyo
Created April 20, 2012 02:13
Show Gist options
  • Save kinopyo/2425339 to your computer and use it in GitHub Desktop.
Save kinopyo/2425339 to your computer and use it in GitHub Desktop.
config.autoload_paths += %W( #{ config.root }/lib/middleware )
config.middleware.use "WwwMiddleware"
# lib/middlewares/middleware.rb
class WwwMiddleware
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.host.starts_with?("www.")
[301, {"Location" => request.url.sub("//www.", "//")}, self]
else
@app.call(env)
end
end
def each(&block)
end
end
@hdoan741
Copy link

You can use [] as the last response value

      [301, {"Location" => request.url.sub("//www.", "//")}, []]

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