Created
July 22, 2011 18:03
-
-
Save dodeja/1100002 to your computer and use it in GitHub Desktop.
Root Domain to WWW redirection in Rails 3.1 before forcing SSL on Heroku
This file contains 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
# in config/environments/production.rb | |
# config.force_ssl = true | |
# config.middleware.use "Forcessl" | |
# Make sure redirection happens before forcing ssl | |
# config.middleware.insert_before 'Rack::SSL', 'Forcessl' | |
# in lib/forcessl.rb | |
# change *. to your domain | |
class Forcessl | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
request = Rack::Request.new(env) | |
if request.host.starts_with?("*.com") | |
[301, {"Location" => request.url.sub("//*.com", "//www.*.com")}, self] | |
else | |
@app.call(env) | |
end | |
end | |
def each(&block) | |
end | |
end |
This was helpful, thank you
Thanks for sharing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code.
Works awesome.