Peter Brown
Peter Brown

Reputation: 51697

How do I Redirect non SSL traffic when SSL is not needed?

I have a section of my application that is using SSL with the force_ssl method, doing something like this in the related controllers:

class MyController < ApplicationController
  force_ssl
end

This works fine, however, on the pages that are not part of this section, I don't want to use SSL. Is it possible to redirect back to the non-SSL protocol when it's not needed?

Upvotes: 0

Views: 78

Answers (2)

user12733
user12733

Reputation: 153

I don't think that there is some automagic function for this. So you will have to had some kind of definitions where SSL is needed and where not. When you make that mapping it is trivial to make simple condition in ApplicationController.

like this, if you don't mind syntax

https_only = { auth:{'login','logout',...}, registration:{'new','edit',...},...}

https_only.each do |controller, actions|
    if controller_name == controller
        actions.each do |a|
            if action_name == a
                do_redirect_to_https_with_current_path
            else
                do_nothing
        end
    end
end

Upvotes: 0

carpamon
carpamon

Reputation: 6623

Why don't you do ssl redirects at an HTTP level? Doing it at an HTTP level is cleaner and faster (as the request is redirected before Rails).

If you see the force_ssl source code (http://apidock.com/rails/ActionController/ForceSSL/ClassMethods/force_ssl) it just makes a simple redirect.

You may also wan't to use the :protocol option of link_to.

Upvotes: 1

Related Questions