SSL Redirection with Rails 3.0.10 from non-www to www and http to https
We have purchased Verisign SSL for a Domain for https://www.doma开发者_Python百科inname.com ; the problem is that it does not work for https://domainname.com. I am using ssl_requirement Gem: https://github.com/rails/ssl_requirement
So I patched the Gem to also redirect non www ones to https://www.domainname.com and it worked well for the following scenarios:
http://www.domainname.com => https://www.domainname.com http://domainname.com => https://www.domainname.com
But not for the scenario where someone types:
https://domainname.com
It seems that the server throws error before Rails takes control. Here are the details of the server:
OS: RHEL 5.5 Ruby: 1.9.2 Rails: 3.0.10 (on RVM) Web Server: Apache with Passenger
Thanks for your help.
Here's the code I use to redirect from domainname.com to www.domainname.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname.com [nc]
RewriteRule (.*) http://www.domainname.com/$1 [R=301,nc]
This goes in the .htaccess file at the root level of the www directory.
Here's another writeup about SSL in Rails 3.1: http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/
You will prob have to handle this at apache level using a .htaccess file in the public dir of your rails app.
for the www rewrite rule see this SO answer: use htaccess to add www with https support
Also if your going to ssl your whole app have a look at Rack::SSL middleware - it handles secure cookies and such. This post has a good write up on using it and also avoiding mixed content warnings.
http://collectiveidea.com/blog/archives/2010/11/29/ssl-with-rails/
Hope this helps.
NOTE: Just got to thinking, this may not work still, if it doesn't you may have to setup a specific VirtualHost to handle that scenario, hopefully not.
I've found the rack-rewrite gem to be particularly helpful here, particularly since Heroku doesn't use .htaccess. Gem: https://github.com/jtrupiano/rack-rewrite
Here's an example configuration in config/environments/production.rb
ExampleApp::Application.configure do
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
r301 /.*/, Proc.new {|path, rack_env| "http://www.#{rack_env['SERVER_NAME']}#{path}" }, :if => Proc.new {|rack_env| rack_env['SERVER_NAME'] !~ /www\./i}
end
#... the rest of production environment config.
end
Check it out as my gist at: https://gist.github.com/1843097
精彩评论