百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rack-canonical-host

> 编程语言
开源

用于定义标准主机名的机架中间件。

396 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于定义标准主机名的机架中间件。

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.

Installation

Add this line to your application’s Gemfile:

ruby
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.

ruby
require 'rack/canonical_host'
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:

ruby
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, which accepts a string, a regular expression, a proc, or an array of those objects.

ruby
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.

ruby
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 within a multi-domain environment, use the :if option, which accepts a string, a regular expression, a lambda or proc, or an array of those objects.

ruby
use Rack::CanonicalHost, 'example.com', if: /.*\.example\.com/
use Rack::CanonicalHost, 'example.org',
  if: ->(uri) { uri.host == 'www.example.org' }

Cache-Control

To avoid browsers indefinitely caching a 301 redirect, it’s a sensible idea to set an expiry on each redirect, to hedge against the chance you may need to change that redirect in the future.

ruby
# Leave caching up to the browser (which could cache it indefinitely):
use Rack::CanonicalHost, 'example.com'

# Cache the redirect for up to an hour:
use Rack::CanonicalHost, 'example.com', cache_control: 'max-age=3600'

# Prevent caching of redirects:
use Rack::CanonicalHost, 'example.com', cache_control: 'no-cache'

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:

  • Pete Nicholls
  • Tyler Ewing
  • Thomas Maurer
  • Jeff Carbonella
  • Joost Schuur
  • Jon Wood
  • Peter Baker
  • Nathaniel Bibler
  • Eric Allam
  • Fabrizio Regini
  • Daniel Searles
  • Sean Huber
  • Olle Jonsson
  • Vinny Diehl
  • Gareth Jones
  • Robert Keresnyei

Copyright

Copyright © 2009 Tyler Hunt.

Released under the terms of the MIT license. See LICENSE for details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言