How to implement a redirection-based gay app?

I would like to create a heroku deployment rack application to create 301 redirects to other subdomains.

If the path was moved, it would be nice.

I started with the following code, but it does not work:

require 'rack-force_domain'

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

run lambda { |env| [200, {'Content-Type'=>'text/plain'}, StringIO.new("Hello World!\n")] }
+3
source share
4 answers

I solved this problem finally using sinatra. What is mine config.ru:

require 'sinatra'

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

run Sinatra::Application
+3
source

Just did it, 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 does not meet your forwarding rules will simply return 200 with the text "Nothing Here!"

+1
source

I adapted the answer by @rriemann. I found that I want the full path to be redirected, including parameters. Also, does not interpolate the string provided by the user, such as dangerous? This works for me:

require 'sinatra'

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

run Sinatra::Application

Also, here is the github link for my implementation in case anyone wants to.

0
source

All Articles