How to Redirect from to on Herok

Hello I'm looking to learn how to redirect all non-www ( to

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( if Rails.env ==

Problem with this is since the SSL cert on Heroku is for,
and not, the browser throws the nasty SSL HTTPS invalid

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


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

Tilde Equals

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


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

This would typically be done by the server (usually Apache).

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.


Have u seen this…?

Thanks and regards,
Shyam Mohan
Ruby on rails developer at CRYPSIS (Gurgaon)
Email :
Mobile : +91-971-618-9650
Web :

Please see this

Thanks and regards,
Shyam Mohan
Ruby on rails developer at CRYPSIS (Gurgaon)
Email :
Mobile : +91-971-618-9650
Web :

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 "" + request.request_uri if !/
^www/.match( if Rails.env == 'production'

No luck though, Chrome still shows the SSL error when you load
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 $$$$