How to Redirect from http://mysite.com to https://www.mysite.com on Herok

Hello I'm looking to learn how to redirect all non-www (mysite.com) to https://www.mysite.com

I tried the following:

class ApplicationController < ActionController::Base

  before_filter :check_uri

  def check_uri     redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host) if Rails.env == 'production'   end

Problem with this is since the SSL cert on Heroku is for www.mysite.com, and not mysite.com, the browser throws the nasty SSL HTTPS invalid warning.

Any ideas on how to redirect in a way that avoids the nasty SSL HTTPs invalid warning?

Thanks

CuriousNewbie, This is going to be more requests, but I think the easiest way to do it is probably to redirect traffic from mysite.com to www.mysite.com, like you're doing, and then redirect traffic from http: to https: using something like ssl_requirement. That way, if somebody visits http://mysite.com/, they'll be redirected to http://www.mysite.com/ and then redirected again to https://www.mysite.com/.

Tilde Equals

I have never used Heroku, but couldn't you just use "https://" in the redirect instead of request.protocol?

Hi,

you can try to modify the ssl_requirements the plugin to fit your need.

Warning: I was using it a couple of years ago and my suggestion may be outdated :slight_smile:

trigger the ssl required with ssl_required :action_name in the controller and modify ensure_proper_protocol to add 'www' when missing:

https://gist.github.com/726486

This would typically be done by the server (usually Apache). http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Agreed, but using Heroku means that it not an option.

Is it possible to do something similar to this little gem? Maybe implemented as Rack middleware.

https://github.com/atmos/rack-redirect/blob/master/lib/rack-redirect.rb

Hi

http://docs.heroku.com/custom-domains

Have u seen this…?

Thanks and regards, Shyam Mohan Ruby on rails developer at CRYPSIS (Gurgaon) Email : shyam@crypsis.net Mobile : +91-971-618-9650 Web : shyam.heroku.com

Please see this

http://docs.heroku.com/custom-domains

Thanks and regards, Shyam Mohan Ruby on rails developer at CRYPSIS (Gurgaon) Email : shyam@crypsis.net Mobile : +91-971-618-9650 Web : shyam.heroku.com

Thanks Shyam, I took at a look at the link but I'm not sure how that would solve the redirect need? Still haven't been able to figure this one out...

Thanks Tim, I tried:

  def check_uri     redirect_to "https://www.mysite.com" + request.request_uri if !/ ^www/.match(request.host) if Rails.env == 'production'   end

No luck though, Chrome still shows the SSL error when you load https://mysite.com It seems to not being taking check_uri in effect early enough.

Any other approaches or ideas? Heroku wasn't able to offer any advice other than buying a wildcard cert $$$$