nuxxxx
nuxxxx

Reputation: 223

http to https redirect on openshift rails app

I want my rails 4.0 app on Openshift Online to serve content only over https. There is a guide which tells to use a .htaccess in the web-root:

RewriteEngine on  

RewriteCond %{HTTP:X-Forwarded-Proto} !https  
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

I followed the guide and put a .htaccess file with the directive in my app-root/repo directory on the openshift cartridge, but nothing happens. The guide talks about a web-root dir. What is the web-root directory of a rails app, or what is a web-root directory on openshift? And is there another way to establish http to https redirect for rails on openshift?

Upvotes: 2

Views: 693

Answers (2)

Oleksii Kyslytsyn
Oleksii Kyslytsyn

Reputation: 2426

There is one solution for Ruby 1.9 for OpenShift 2 with steps for terminal:

cd your_ruby_git_project_folder/public/

vi .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

cd ..

git add . -A

git commit -m 'message'

git push

Upvotes: 0

Paul B.
Paul B.

Reputation: 85

I was in a similar situation as you (but wanting to redirect depending on the Accept-Language http header), and could not find the place where to put the .htaccess on an OpenShift ruby cartridge.

Tentative with .htaccess

I tried to put the .htaccess file in the app-root/repo, app-root/runtime, app-root/data, the ~/ruby/ directories with no success..

So I ended up doing the redirections from the rails app

A solution for your case through Rails

If you want to enforce SSL for all your application you can simply set config.force_ssl = true in your config/application.rb file. Another common use case is to enforce SSL only for your production environment, thus instead of putting this configuration in your general application.rb file, you can set it in your config/environments/production.rb.

In case you need to enforce SSL only for specific controllers, you could also just call the force_ssl method in the top of your controller as:

class MyController < ApplicationController
  force_ssl
  [...your actions..]
end

My problem & solution through Rails

I wanted to add this .htaccess

RewriteEngine On

RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule ^/$ /pt/ [L,R=301]

RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^/$ /fr/ [L,R=301]

RewriteRule ^/$ /en/ [L,R=301]

And finally had to do it in the config/routes.rb config file of rails with

get '/', to: redirect { |path_params, req|
"/#{req.env['HTTP_ACCEPT_LANGUAGE'].scan(/^(?:pt|fr)/).first || 'en'}" }

Any help from the OpenShift team to explain where to put a .htaccess for a Ruby cartridge is still very welcome!

Upvotes: 2

Related Questions