Foreign Domain Routing +
Request Routing Plugin for Ruby on Rails
= Foreign Domain Request Routing
Foreign Domain Routing provides simple handling of foreign domains in Rails.
It borrows largely from SubdomainFu and some other snippets found around the web.
Request routing allows you to define routing conditions that test
methods/properties of the request object such as subdomain, domain,
port. You can test them either against a value or with a Regexp
(assuming the method returns a string)
Merging these two plugins allows them to work together and enhance the routing
capability of Rails.
Documentation from the original plugins follows...
Foreign Domain Routing
Installation
Foreign Domain Routing is available as a plugin. To install it with Rails 2.1
or later):
script/plugin install git://github.com/brianmulloy/foreign-domain-routing.git
Examples
Foreign Domain Routing extends Rails's routing mechanisms to provide a way to
redirect non-native domains.
Let's say my rails app domain is 'mydomain.com' and I am creating an app that
allows users to add a CNAME record to map their subdomain so that
'foo.usersdomain.com' would actually point to 'mydomain.com/users/1234'.
The route at the top of config/routes.rb would look like:
map.connect '*path', :controller => 'users',
:action => 'index', :conditions => { :foreign_domain => true }
And in the users controller:
@user = User.find_by_foreign_domain(request.host.downcase)
this example would require a database field called foreign_domain
Configuration
You will need to configure Foreign Domain Routing based on your native hostnames.
native_domains
A hash of arrays for the native domain names for each relevant environment.
Create the file config/initializers/native_domains.rb and put something like:
ForeignDomainRouting.init_native_domains = {
:development => ['localhost'],
:staging => ['staging.example.com'],
:production => ['example.com', 'example.org', 'example.net']
} # set all at once (also the defaults)
Or set the native domains on the fly with:
ForeignDomainRouting.native_domains =
['example.com', 'example.org', 'example.net'] # sets for current environment
Resources
Request Routing Plugin for Ruby on Rails
(c) Dan Webb 2006 (dan@vivabit.com)
Plugin that allows you to define routing conditions that test
methods/properties of the request object such as subdomain, domain,
port. You can test them either against a value or with a Regexp
(assuming the method returns a string)
UPDATE Now works with the new routing code as implemented in edge rails. Note the
change in API: use :conditions instead of :requirements.
== Installation
ruby script/plugin install http://svn.vivabit.net/external/rubylibs/request_routing/
== Usage
In routes.rb you can specify use the :requirements hash with request properties:
map.connect '', :controller => 'main', :conditions => { :subdomain => 'www' }
map.connect 'admin', :controller => 'admin', :conditions => { :remote_ip => /^127\.0\.0\.[0-9]$/ }
You can also, of course, use the conditions hash in map.with_options calls.
The allowed properties are:
:subdomain (only checks the first subdomain)
:domain (only accurate for single tld domain names at the moment)
:method (a symbol)
:port (a number)
:remote_ip
:content_type (content type of the post body)
:accepts
:request_uri (the entire request uri)
:protocol (either http:// or https://)
== Copyright
Copyright (c) 2009 Joe Scharf. See LICENSE for details.