Giter VIP home page Giter VIP logo

rack-canonical-host's Introduction

Rack Canonical Host

Rack middleware that lets you define a single host name as the canonical host for your application. Requests for other host names will then be redirected to the canonical host.

Build Status

Installation

Add this line to your application's Gemfile:

gem 'rack-canonical-host'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-canonical-host

Usage

For most applications, you can insert the middleware into the config.ru file in the root of the application.

Here's a simple example of what the config.ru in a Rails application might look like after adding the Rack::CanonicalHost middleware.

require ::File.expand_path('../config/environment',  __FILE__)

use Rack::CanonicalHost, 'example.com'
run YourRailsApp::Application

In this case, any requests coming in that aren't for the specified host, example.com, will be redirected, keeping the requested path intact.

Environment-Specific Configuration

You probably don't want your redirect to happen when developing locally. One way to prevent this from happening is to use environment variables in your production environment to set the canonical host name.

With Heroku, you would do this like so:

$ heroku config:add CANONICAL_HOST=example.com

Then, can configure the middleware like this:

use Rack::CanonicalHost, ENV['CANONICAL_HOST'] if ENV['CANONICAL_HOST']

Now, the middleware will only be used if a canonical host has been defined.

Options

If you'd like the middleware to ignore certain hosts, use the :ignore option:

use Rack::CanonicalHost, 'example.com', ignore: ['api.example.com']

In this case, requests for the host api.example.com will not be redirected.

Alternatively, you can pass a block whose return value will be used as the canonical host name.

use Rack::CanonicalHost do |env|
  case env['RACK_ENV'].to_sym
    when :staging then 'staging.example.com'
    when :production then 'example.com'
  end
end

If you want it to react only on specific hosts for multi-domain environment use :if, it accepts string, regexp or an array.

use Rack::CanonicalHost, 'example.com', if: /.*\.example\.com/
use Rack::CanonicalHost, 'example.ru', if: /.*\.example\.ru/

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature.')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Thanks to the following people who have contributed patches or helpful suggestions:

Copyright

Copyright © 2009-2012 Tyler Hunt. Released under the terms of the MIT license. See LICENSE for details.

rack-canonical-host's People

Contributors

finack avatar firedev avatar jellybob avatar jschuur avatar nbibler avatar tylerhunt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.