Skip to content

Instantly share code, notes, and snippets.

@dodeja
Created July 22, 2011 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dodeja/1100002 to your computer and use it in GitHub Desktop.
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
# 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
@adriancuadros
Copy link

Thanks for the code.

Works awesome.

@JonasNielsen
Copy link

This was helpful, thank you

@heijmerikx
Copy link

Thanks for sharing.

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