https://github.com/gbuesing/rack-host-redirect
Rack middleware to redirect legacy domains
https://github.com/gbuesing/rack-host-redirect
rack rack-middleware ruby
Last synced: about 1 month ago
JSON representation
Rack middleware to redirect legacy domains
- Host: GitHub
- URL: https://github.com/gbuesing/rack-host-redirect
- Owner: gbuesing
- License: mit
- Created: 2013-06-10T19:24:52.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2018-06-06T13:24:51.000Z (over 7 years ago)
- Last Synced: 2025-08-22T14:45:48.394Z (about 2 months ago)
- Topics: rack, rack-middleware, ruby
- Language: Ruby
- Homepage:
- Size: 21.5 KB
- Stars: 82
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
Rack::HostRedirect
==================A lean and simple Rack middleware that 301 redirects requests from one host to another.
This is useful for environments where it's difficult or impossible to implement this via Nginx/Apache configuration (e.g. Heroku.)
I'm using this to redirect traffic from a *.herokuapp.com subdomain to a custom domain, and to redirect the www subdomain to the bare domain.
Configuration below is for Rails, but this middleware should also work just fine with Sinatra and bare Rack apps.
Rails configuration
-------------------in Gemfile:
```ruby
gem 'rack-host-redirect'
```in config/environments/production.rb:
```ruby
config.middleware.use Rack::HostRedirect, {
'myapp.herokuapp.com' => 'www.myapp.com'
}
```With this configuration, all requests to ```myapp.herokuapp.com``` will be 301 redirected to ```www.myapp.com```.
Path, querystring and protocol are preserved, so a request to:
https://myapp.herokuapp.com/foo?bar=baz
will be 301 redirected to:
https://www.myapp.com/foo?bar=baz
Addtional host redirections can be specified as key-value pairs in the host mapping hash:
```ruby
config.middleware.use Rack::HostRedirect, {
'myapp.herokuapp.com' => 'www.myapp.com',
'old.myapp.com' => 'new.myapp.com'
}
```Multiple hosts that map to the same redirect destination host can be specified by an Array key:
```ruby
config.middleware.use Rack::HostRedirect, {
%w(myapp.herokuapp.com foo.myapp.com) => 'www.myapp.com'
}
```URI methods to set for redirect location can be specified as a hash value:
```ruby
# Don't preserve path or query on redirect:
config.middleware.use Rack::HostRedirect, {
'bar.myapp.com' => {host: 'www.myapp.com', path: '/', query: nil}
}
```When specifying a URI methods hash, the ```:host``` key is required; all other URI keys are optional.
Skip redirect for special cases (e.g. Let's Encrypt)
---When you specify a host redirect, it will redirect all requests to that host, but if you need certain exclusions to this -- e.g. you're handling a Let's Encrypt challenge request in your app, so you need to let that pass through -- you can do so via passing in a Proc to the ```:exclude``` key:
```ruby
# Allow ACME challenge request to pass through, redirect everything else:
config.middleware.use Rack::HostRedirect, {
'www.example.com' => {
host: 'example.com',
exclude: -> (request) { request.path.start_with?('/.well-known/acme-challenge/') }
}
}
```The proc will be called for each request with the ```Rack::Request``` object. If the proc returns a truthy value, the request will pass through without a redirect.
With ActionDispatch::SSL
------------------------If your app is setting ```config.force_ssl = true``` and you're redirecting from a domain for which you don't maintain certs, you should insert ```Rack::HostRedirect``` ahead of ```ActionDispatch::SSL``` in the middleware stack, so that the redirect happens before the SSL upgrade:
```ruby
config.middleware.insert_before ActionDispatch::SSL, Rack::HostRedirect, {
'www.legacy-domain.com' => 'www.myapp.com'
}
```