I'm using a wildcard certificate with Heroku for my Rails 4.2 project which provides users subdomains for their accounts. I've got it almost working perfectly but have a few kinks to iron out.
If I type http://example.com in the brower, it get's redirected to https://www.example.com and resolves fine.
However if I type https://example.com I get the following in my browser
Unable to connect. Firefox can't establish a connection to the server at mydomain.com. The site could be temporarily unavailable or too busy. Try again in a few moments....
These are my DNS settings
@ ----> http://www.example.com URL-REDIRECT (This is intentional - Heroku cant use naked domains with SSL)
www ----> blahblah-1234.herokussl.com CNAME
subdomains
* ----> blahblah-1234.herokussl.com. CNAME
As well as the DNS 'url-redirect' mentioned above I have also configured rails to rewrite rootdomain requests. (I dont think the reqest is even hitting rails though)
I have created a middleware to rewrite requests 'example.com' to 'www.example.com' and inserted it before ActionDispatch::SSL
class RootToWWW
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.host.starts_with?('myexample.com')
[301, {"Location" => request.url.sub("//", "//www.")}, self]
else
@app.call(env)
end
end
def each(&block)
end
end
# in production.rb
config.middleware.insert_before 'ActionDispatch::SSL', 'RootToWWW'
Can anyone explain how I can ensure https requests to root domains get redirected to www?