Skip to content

Rack::Redirect, generic Rack redirect middleware.

Notifications You must be signed in to change notification settings

koenpunt/rack-redirect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rack::Redirect, generic Rack redirect middleware.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published