diff --git a/README.textile b/README.textile index f14926c..f3752fe 100644 --- a/README.textile +++ b/README.textile @@ -232,7 +232,8 @@ and tweet some praise. I'd love to know you were helped out by what I've put tog h4. Contributors -A big thank you to contributor Fred Schoeneman for improving the tutorial. +Thank you to contributor Fred Schoeneman for improving the tutorial. +Thank you to contributor Charlie Ussery for suggesting how to ignore the "www" subdomain. h2. License diff --git a/TUTORIAL.textile b/TUTORIAL.textile index d0935bf..efb7f93 100644 --- a/TUTORIAL.textile +++ b/TUTORIAL.textile @@ -130,7 +130,7 @@ The application uses the following gems: * rails (version 3.0.0) * sqlite3-ruby -* devise (version 1.1.3) +* devise (version 1.1.2) * friendly_id (version 3.1.6) The SQLite3 gem is used for the database. You can substitute a different database if you wish. @@ -1013,7 +1013,25 @@ end h2. Ignore a "www" Subdomain -In his screencast "Subdomains in Rails 3":http://railscasts.com/episodes/221-subdomains-in-rails-3, Ryan Bates shows how to create a routing constraint that will redirect a "www" URL to the main home page. Unfortunately the implementation doesn't work in the Rails 3 release candidate. If you have a solution or workaround, please open an "issue":http://github.com/fortuity/rails3-subdomain-devise/issues on GitHub and I will update this example to include it. +At this point, if we visit "http://www.lvh.me:3000/":http://www.lvh.me:3000/ our application will look for a site with the subdomain "www" which doesn't exist. Instead this should redirect to the main home page. + +In his screencast "Subdomains in Rails 3":http://railscasts.com/episodes/221-subdomains-in-rails-3, Ryan Bates shows how to create a routing constraint that will redirect a "www" URL to the main home page. His solution requires writing an auxiliary class but there's a simpler solution. + +Here's how to create a routing constraint that will redirect a "www" URL to the main home page. + +Edit the file *config/routes.rb* to look like this: + +
+devise_for :users
+resources :users, :only => [:index, :show] do
+  resources :subdomains, :shallow => true
+end
+match '/' => 'home#index', :constraints => { :subdomain => 'www' }
+match '/' => 'sites#show', :constraints => { :subdomain => /.+/ }
+root :to => "home#index"
+
+ +This will intercept the "www" subdomain first, then the @limit_subdomain_access@ method in the ApplicationController will redirect to root, effectively getting rid of the "www". Note that you will need to add your own code to prevent users from creating a subdomain called "www". h2. Maintaining Sessions Across Subdomains @@ -1087,4 +1105,5 @@ and tweet some praise. I'd love to know you were helped out by the tutorial. h4. Contributors -A big thank you to contributor Fred Schoeneman for improving the tutorial. +Thank you to contributor Fred Schoeneman for improving the tutorial. +Thank you to contributor Charlie Ussery for suggesting how to ignore the "www" subdomain. diff --git a/config/routes.rb b/config/routes.rb index d39b9da..926dfe3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ resources :users, :only => [:index, :show] do resources :subdomains, :shallow => true end + match '/' => 'home#index', :constraints => { :subdomain => 'www' } match '/' => 'sites#show', :constraints => { :subdomain => /.+/ } root :to => "home#index" diff --git a/template.rb b/template.rb index a8945ec..5043962 100644 --- a/template.rb +++ b/template.rb @@ -724,6 +724,7 @@ def limit_subdomain_access resources :users, :only => [:index, :show] do resources :subdomains, :shallow => true end + match '/' => 'home#index', :constraints => { :subdomain => 'www' } match '/' => 'sites#show', :constraints => { :subdomain => /.+/ } root :to => "home#index" RUBY