Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koenpunt/rack-redirect
Rack::Redirect, generic Rack redirect middleware.
https://github.com/koenpunt/rack-redirect
middleware rack rack-redirect user-agent
Last synced: about 1 month ago
JSON representation
Rack::Redirect, generic Rack redirect middleware.
- Host: GitHub
- URL: https://github.com/koenpunt/rack-redirect
- Owner: koenpunt
- Created: 2016-03-20T15:31:24.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-03-20T15:52:59.000Z (over 8 years ago)
- Last Synced: 2024-04-29T11:04:56.089Z (7 months ago)
- Topics: middleware, rack, rack-redirect, user-agent
- Language: Ruby
- Homepage:
- Size: 3.91 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this 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:
```ruby
gem 'rack_redirect'
```And then execute:
$ bundle
## Usage
### Rackup
Add to `config.ru`:
```rb
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):
```rb
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:
```rb
class UserAgentRedirect < Rack::Redirect
def request_matches?(request, pattern)
request.user_agent =~ pattern
end
enduse 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:
```rb
class UserAgentAndPathRedirect < Rack::Redirect
def request_matches?(request, pattern)
request.user_agent =~ pattern[0] && request.path == pattern[1]
end
enduse 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.