Created
April 20, 2012 02:13
-
-
Save kinopyo/2425339 to your computer and use it in GitHub Desktop.
Rails middleware: redirect www.example.com to example.com (root domain), taken from http://blog.dynamic50.com/2011/02/22/redirect-all-requests-for-www-to-root-domain-with-heroku/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.autoload_paths += %W( #{ config.root }/lib/middleware ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.middleware.use "WwwMiddleware" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
@max without each
method I got this error
[2018-08-16 19:01:45] ERROR NoMethodError: undefined method `each' for #<MyCustomElevator:0x00000005c1cca8>
/home/john/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/body_proxy.rb:31:in `each'
/home/john/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/body_proxy.rb:31:in `each'
/home/john/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/body_proxy.rb:31:in `each'
/home/john/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/body_proxy.rb:31:in `each'
/home/john/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/body_proxy.rb:31:in `each'
/home/john/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/body_proxy.rb:31:in `each'
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
Hello,
I would like to know what is the each block code for?
Could you explain?