0.0
No commit activity in last 3 years
No release in over 3 years
Rack middleware to redirect requests based on path, but can be easily extended to match hostname, user-agent, or whatever might be available in the request.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0

Runtime

~> 1.4
 Project Readme

Rack::Redirect

Rack::Redirect, generic Rack redirect middleware.

By default to redirect requests based on path, but can be easily extended to match hostname, user-agent, or whatever might be available in the request.

Installation

Add this line to your application's Gemfile:

gem 'rack_redirect'

And then execute:

$ bundle

Usage

Rackup

Add to config.ru:

require 'rack/redirect'
use Rack::Redirect, {
  '/some/path'    => '/new/path',
  %r{^/profiles}  => '/users'
}

use Rack::Redirect, {
  '/home' => '/'
}, 301

Rails

Add to config/application.rb (or any of the environment files):

config.middleware.insert_before 0, Rack::Redirect, {
  '/some/path'    => '/new/path',
  %r{^/profiles}  => '/users'
}

config.middleware.insert_before 0, Rack::Redirect, {
  '/home' => '/'
}, 301

Extending

To extend the functionality for e.g. user-agent detection, it's as simple as:

class UserAgentRedirect < Rack::Redirect
  def request_matches?(request, pattern)
    request.user_agent =~ pattern
  end
end

use UserAgentRedirect, {
  /iP(hone|od)/ => '/mobile'
}

The pattern can actually be anything, so if you wan't use more variables in a single check, that's also possible:

class UserAgentAndPathRedirect < Rack::Redirect
  def request_matches?(request, pattern)
    request.user_agent =~ pattern[0] && request.path == pattern[1]
  end
end

use UserAgentAndPathRedirect, {
  [/iP(hone|od)/, '/mobile'] => '/iphone'
}

Development

After checking out the repo, run bin/setup to install dependencies.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/koenpunt/rack-redirect.