Forcing urls to use https

I found this extremely useful:

1
2
3
def redirect_to_ssl
    redirect_to url_for params.merge({:protocol => 'https://'}) unless request.ssl?
end

Put this in your application controller and call


before_filter :redirect_to_ssl 

on every controller that would require ssl. You can pick which actions would only require this by extending your before_filter declaration as such:


before_filter :redirect_to_ssl, :only => [:new, :purchase]

or even by exclusion, whichever is more convenient for your case:


before_filter :redirect_to_ssl, :except => [:show]

PS: There are other resources too, such as this one (that could be of help): http://fuadcse.blogspot.com/2009/01/redirecting-http-request-to-https-in.html

Recommend me on Working With Rails

If you found this post of any use, then please take the time to recommend me on Working with Rails.

Leave a Reply

Hi, I've been busy you know. wink If you don't know yet, this site and my blog codes are hosted in . I recently moved to Git and found that they have support for private Git repositories. Below are details of my last git push.

Maricris S. Nonato on 14 Feb
Commit: 48fe3652ffb644af13f4a5651f9d80474b09f622

Adding new ad image for Pledgie campaign - campaign image for coderkitty fundraising
  • favorited a lol 'Harry's tuxedo seemed a bit snug...' http://twlol.com/tw/..?a3294720768 #lol #ichc 2010-03-21
  • favorited a lol 'Invisible bike hits invisible scooter!' http://twlol.com/tw/..?a3302849280 #lol #ichc 2010-03-21
  • just updated "The world should know" post. 2010-03-21
  • Reggie: the doble-kara singer contestant in Pilipinas Got Talent, you are awesome! smile 2010-03-20
  • dear mama, i would give you anything you ask, do anything for you, as long as you make me cups of hot tea everyday. smile #fb 2010-03-20
  • LivingLife :: The Space Between: Ambient music takes me to a point of silence and peace inside my head.  Someti... http://bit.ly/cg52Sj.. #fb 2010-03-20
  • false hopes. 2010-03-20
  • @AdeMagnaye ahahaha. that just made me laugh tongue 2010-03-20
  • i miss JP sad :( :( 2010-03-19
  • frustration on books/ebooks and ereaders kicking in.. sigh. got to get my mind off them for now 2010-03-19
  • i am worth more than how you see me. my faith and belief in it is greater today than yesterday #fb 2010-03-19
  • nice.. http://bit.ly/dAr1Co.. 2010-03-19
  • There can be no happiness if the things we believe in are different from the things we do. - Freya Stark #fb 2010-03-15
  • LivingLife :: My first cabby receipt: Today seems a normal day.  I have good vibes for today.  smile  At first I f... http://bit.ly/cAZJiL.. #fb 2010-03-15
  • would you believe that Filipinos "trust" Lea Salonga second to Rosa Rosal (in Reader's Digests' Who do you trust poll)? weird. 2010-03-12
  • have you seen a chinese owl? i can't get this image out of my head: http://bit.ly/czilyD.. 2010-03-12