3

I would like to create an rack application for deployment of heroku to create some 301 redirects to other subdomains.

If the path would be carried over, it would be nice.

I started with the following code, but it isn't working:

require 'rack-force_domain'

use Rack::ForceDomain, ENV["DOMAIN"]

run lambda { |env| [200, {'Content-Type'=>'text/plain'}, StringIO.new("Hello World!\n")] }
rriemann
  • 528
  • 4
  • 17

4 Answers4

3

I solved this problem by using sinatra finally. That's my config.ru:

require 'sinatra'

get %r{(.*)} do
  redirect to("http://custom.domain#{params[:captures].first}"), 301
end

run Sinatra::Application
rriemann
  • 528
  • 4
  • 17
1

Just did this and it was surprisingly simple:

Gemfile:

source 'https://rubygems.org'

ruby '2.0.0'

gem 'rack'
gem 'rack-rewrite'

config.ru:

require 'rack/rewrite'

use Rack::Rewrite do
  ...
end

run lambda { |env| [200, {'Content-Type'=>'text/plain'}, StringIO.new("Nothing Here!\n")] }

Anything that doesn't match your redirect rules will just return a 200 with text "Nothing Here!"

swrobel
  • 4,053
  • 2
  • 33
  • 42
0

Take a look at rack-rewrite - https://github.com/jtrupiano/rack-rewrite, it has an example for what you want to achieve.

John Beynon
  • 37,398
  • 8
  • 88
  • 97
  • That's how my `config.ru` looks like now. require 'rack/rewrite' use Rack::Rewrite do r301 %r{.*}, 'http://blog.example.com/$&' end Heroku logs reports: Unexpected error while processing request: missing run or map statement – rriemann May 21 '12 at 16:24
0

I adapted the answer by @rriemann a bit. I found that I want the full path to be redirected including params. Also, isn't interpolating a user supplied string like that dangerous? This works for me:

require 'sinatra'

get "*" do
  redirect to("http://custom.domain" + request.fullpath), 301
end

run Sinatra::Application

Also, here is a github link to my implementation in case anyone wants it.

winni2k
  • 1,460
  • 16
  • 19