All Projects β†’ gbuesing β†’ Rack Host Redirect

gbuesing / Rack Host Redirect

Licence: mit
Rack middleware to redirect legacy domains

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Rack Host Redirect

Lamby
Simple Rails & AWS Lambda Integration πŸ‘πŸ›€
Stars: ✭ 336 (+425%)
Mutual labels:  rack
Rack Tracker
Tracking made easy: Don’t fool around with adding tracking and analytics partials to your app and concentrate on the things that matter.
Stars: ✭ 601 (+839.06%)
Mutual labels:  rack
Redis Rack
Redis session store for Rack
Stars: ✭ 46 (-28.12%)
Mutual labels:  rack
Client ruby
Prometheus instrumentation library for Ruby applications
Stars: ✭ 369 (+476.56%)
Mutual labels:  rack
Rack
A modular Ruby web server interface.
Stars: ✭ 4,260 (+6556.25%)
Mutual labels:  rack
Iodine
iodine - HTTP / WebSockets Server for Ruby with Pub/Sub support
Stars: ✭ 720 (+1025%)
Mutual labels:  rack
Secure headers
Manages application of security headers with many safe defaults
Stars: ✭ 2,942 (+4496.88%)
Mutual labels:  rack
Health bit
Tiny health check of Rack apps like Rails, Sinatra for use with uptime checking systems like Kubernetes, Docker or Uptimerobot
Stars: ✭ 60 (-6.25%)
Mutual labels:  rack
Rack Attack
Rack middleware for blocking & throttling
Stars: ✭ 5,012 (+7731.25%)
Mutual labels:  rack
Open api parser
A parser for Open API specifications
Stars: ✭ 30 (-53.12%)
Mutual labels:  rack
Raxx
Interface for HTTP webservers, frameworks and clients
Stars: ✭ 378 (+490.63%)
Mutual labels:  rack
Async sinatra
A plugin for Sinatra to provide a DSL extension for using Thin for asynchronous responses
Stars: ✭ 434 (+578.13%)
Mutual labels:  rack
Rack Throttle
Rack middleware for rate-limiting incoming HTTP requests.
Stars: ✭ 898 (+1303.13%)
Mutual labels:  rack
Rack Dev Mark
Show dev mark on development env
Stars: ✭ 350 (+446.88%)
Mutual labels:  rack
Rackula
Generate a static site from any rack middleware.
Stars: ✭ 49 (-23.44%)
Mutual labels:  rack
Stubb
Specify REST API stubs using your file system.
Stars: ✭ 289 (+351.56%)
Mutual labels:  rack
Agoo
A High Performance HTTP Server for Ruby
Stars: ✭ 679 (+960.94%)
Mutual labels:  rack
Nginx omniauth adapter
Use omniauth for nginx auth_request
Stars: ✭ 63 (-1.56%)
Mutual labels:  rack
Letsencrypt heroku
Automated letsencrypt setup for heroku
Stars: ✭ 58 (-9.37%)
Mutual labels:  rack
Puma
A Ruby/Rack web server built for parallelism
Stars: ✭ 6,924 (+10718.75%)
Mutual labels:  rack

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:

gem 'rack-host-redirect'

in config/environments/production.rb:

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:

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:

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:

# 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:

# 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:

config.middleware.insert_before ActionDispatch::SSL, Rack::HostRedirect, {
  'www.legacy-domain.com' => 'www.myapp.com'
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].