# ssl\_requirement does not redirect to https

**URL:** <https://discuss.rubyonrails.org/t/ssl-requirement-does-not-redirect-to-https/25169>\
**Category:** rubyonrails-talk\
**Created:** [February 20, 2008, 2:30am UTC](https://discuss.rubyonrails.org/t/ssl-requirement-does-not-redirect-to-https/25169 "2008-02-20T02:30:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobby1](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@bobby1](https://discuss.rubyonrails.org/u/bobby1)\
**Post date:** [February 20, 2008, 2:30am UTC](https://discuss.rubyonrails.org/t/ssl-requirement-does-not-redirect-to-https/25169/1 "2008-02-20T02:30:50Z")

</div>

I'm trying to get SSL working on my app, using ssl\_requirement. SSL is working, but ssl\_requirement doesn't seem to be handling the request properly- it doesn't redirect to https.

Relevant files:

application.rb: class ApplicationController \< ActionController::Base &nbsp;&nbsp;include SslRequirement &nbsp;&nbsp;include AuthenticatedSystem

&nbsp;&nbsp;def ssl\_required? &nbsp;&nbsp;&nbsp;&nbsp;return false if local\_request? || RAILS\_ENV == 'test' &nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;end ... end

users\_controller.rb: class UsersController \< ApplicationController &nbsp;&nbsp;ssl\_required :new, :create, :reset\_password

... end

vhost.conf: ( VirtualHost \*:80 is a duplicate of the code below, except for the first 2 lines) \<VirtualHost 123.123.123.123:443\> SSLEngine on RequestHeader set X\_FORWARDED\_PROTO "https"

DocumentRoot /var/www/apps/my\_app/current/public

&nbsp;&nbsp;\<Directory /var/www/apps/my\_app/current/public\> &nbsp;&nbsp;&nbsp;&nbsp;Options FollowSymLinks &nbsp;&nbsp;&nbsp;&nbsp;AllowOverride None &nbsp;&nbsp;&nbsp;&nbsp;Order allow,deny &nbsp;&nbsp;&nbsp;&nbsp;Allow from all &nbsp;&nbsp;\</Directory\>

&nbsp;&nbsp;# Configure mongrel\_cluster &nbsp;&nbsp;\<Proxy balancer://my\_app\_cluster\> &nbsp;&nbsp;&nbsp;&nbsp;BalancerMember [http://127.0.0.1:8000](http://127.0.0.1:8000) &nbsp;&nbsp;&nbsp;&nbsp;BalancerMember [http://127.0.0.1:8001](http://127.0.0.1:8001) &nbsp;&nbsp;\</Proxy\>

&nbsp;&nbsp;RewriteEngine On

&nbsp;&nbsp;# Prevent access to .svn directories &nbsp;&nbsp;RewriteRule ^(.\*/)?\.svn/ - [F,L] &nbsp;&nbsp;ErrorDocument 403 "Access Forbidden"

&nbsp;&nbsp;# Check for maintenance file and redirect all requests &nbsp;&nbsp;RewriteCond %{DOCUMENT\_ROOT}/system/maintenance.html -f &nbsp;&nbsp;RewriteCond %{SCRIPT\_FILENAME} !maintenance.html &nbsp;&nbsp;RewriteRule ^.\*$ /system/maintenance.html [L]

&nbsp;&nbsp;# Rewrite index to check for static &nbsp;&nbsp;RewriteRule ^/$ /index.html [QSA]

&nbsp;&nbsp;# Rewrite to check for Rails cached page &nbsp;&nbsp;RewriteRule ^([^.]+)$ $1.html [QSA]

&nbsp;&nbsp;# Redirect all non-static requests to cluster &nbsp;&nbsp;RewriteCond %{DOCUMENT\_ROOT}/%{REQUEST\_FILENAME} !-f &nbsp;&nbsp;RewriteRule ^/(.\*)$ balancer://my\_app\_cluster%{REQUEST\_URI} [P,QSA,L]

&nbsp;&nbsp;# Deflate &nbsp;&nbsp;AddOutputFilterByType DEFLATE text/html text/plain text/xml &nbsp;&nbsp;BrowserMatch ^Mozilla/4 gzip-only-text/html &nbsp;&nbsp;BrowserMatch ^Mozilla/4\.0[678] no-gzip &nbsp;&nbsp;BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

&nbsp;&nbsp;ErrorLog logs/my\_app.com-error\_log &nbsp;&nbsp;CustomLog logs/my\_app.com-access\_log combined

&nbsp;&nbsp;# This just specifies locations of key and crt files &nbsp;&nbsp;Include /etc/httpd/conf/apps/ssl.conf \</VirtualHost\>

In production, the app just throws a 404 when I try to access [https://my](https://my)\_app.com/signup.

My specs:

describe "Requesting /signup" do &nbsp;&nbsp;controller\_name :users

&nbsp;&nbsp;before(:each) do &nbsp;&nbsp;&nbsp;&nbsp;@user = mock\_model(User, :to\_param =\> "1", :save =\> true) &nbsp;&nbsp;&nbsp;&nbsp;User.stub!(:new).and\_return(@user) &nbsp;&nbsp;end

&nbsp;&nbsp;def do\_get &nbsp;&nbsp;&nbsp;&nbsp;get :new &nbsp;&nbsp;end

&nbsp;&nbsp;it "should redirect to HTTPS version if request.ssl? is false" do &nbsp;&nbsp;&nbsp;&nbsp;request.stub!(:ssl?).and\_return false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do\_get &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.should redirect\_to("[https://test.host/signup&quot;\](https://test.host/signup&quot;%5C)) &nbsp;&nbsp;end

&nbsp;&nbsp;it "should redirect to the HTTPS version" do &nbsp;&nbsp;&nbsp;&nbsp;request.stub!(:ssl?).and\_return true &nbsp;&nbsp;&nbsp;&nbsp;do\_get &nbsp;&nbsp;&nbsp;&nbsp;response.should redirect\_to("[https://test.host/signup&quot;\](https://test.host/signup&quot;%5C)) &nbsp;&nbsp;end end

autotest spits out: 'Requesting /signup should redirect to the HTTPS version' FAILED expected redirect to "[https://test.host/signup&quot;](https://test.host/signup&quot;), got redirect to "[http://test.host/signup&quot](http://test.host/signup&quot);

'Requesting /signup (/users/new) should redirect to HTTPS version if request.ssl? is false' FAILED expected redirect to "[https://test.host/signup&quot;](https://test.host/signup&quot;), got no redirect

When I try to access the production site via the command line, I get this: $ curl -I [https://my](https://my)\_app.com/signup HTTP/1.1 302 Moved Temporarily Server: Mongrel 1.0.1 Status: 302 Found Location: [http://my](http://my)\_app.com/signup Cache-Control: no-cache Content-Type: text/html; charset=utf-8 Content-Length: 107 Set-Cookie: \_my\_app\_session\_id=7eeea00b749ef2ed8b06730b18c62646; path=/ Vary: Accept-Encoding Connection: close

$ curl -I [http://my](http://my)\_app.com/signup HTTP/1.1 200 OK Server: Mongrel 1.0.1 Status: 200 OK Cache-Control: no-cache Content-Type: text/html; charset=utf-8 Vary: Accept-Encoding Content-length: 12031 Connection: Keep-Alive Set-Cookie: \_my\_app\_session\_id=5c8fd1c3f962b65aeeb6a4b6299c3e46; path=/

The request is getting past Apache, it looks like the app itself is not handling the request correctly.

Has anyone encountered this problem before? Any help would be much appreciated.

Bobby

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [February 20, 2008, 8:48am UTC](https://discuss.rubyonrails.org/t/ssl-requirement-does-not-redirect-to-https/25169/2 "2008-02-20T08:48:27Z")

</div>

try to comment out &nbsp;&nbsp;def ssl\_required? &nbsp;&nbsp;&nbsp;&nbsp;return false if local\_request? || RAILS\_ENV == 'test' &nbsp;&nbsp;&nbsp;&nbsp;super &nbsp;&nbsp;end

In your test env, you disable ssl with the above code.

I usually set ENV['USE\_SSL'] in my env., then application\_controller picks it up in ssl\_required?.

---

<div class="post-metadata">

**Author:** ![bobby1](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@bobby1](https://discuss.rubyonrails.org/u/bobby1)\
**Post date:** [February 20, 2008, 10:17pm UTC](https://discuss.rubyonrails.org/t/ssl-requirement-does-not-redirect-to-https/25169/3 "2008-02-20T22:17:04Z")

</div>

Thanks, I'll try that. Further debugging also indicates a conflict with a plugin, will just have to find out which.
