Skip to content

Commit

Permalink
incorporate suggestion from Charlie Ussery for ignoring the 'www' sub…
Browse files Browse the repository at this point in the history
…domain
  • Loading branch information
fortuity committed Sep 6, 2010
1 parent 05dcfc5 commit c877fbb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.textile
Expand Up @@ -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 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 h2. License


Expand Down
25 changes: 22 additions & 3 deletions TUTORIAL.textile
Expand Up @@ -130,7 +130,7 @@ The application uses the following gems:


* rails (version 3.0.0) * rails (version 3.0.0)
* sqlite3-ruby * sqlite3-ruby
* devise (version 1.1.3) * devise (version 1.1.2)
* friendly_id (version 3.1.6) * friendly_id (version 3.1.6)


The SQLite3 gem is used for the database. You can substitute a different database if you wish. The SQLite3 gem is used for the database. You can substitute a different database if you wish.
Expand Down Expand Up @@ -1013,7 +1013,25 @@ end


h2. Ignore a "www" Subdomain 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:

<pre>
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"
</pre>

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 h2. Maintaining Sessions Across Subdomains


Expand Down Expand Up @@ -1087,4 +1105,5 @@ and tweet some praise. I'd love to know you were helped out by the tutorial.


h4. Contributors 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.
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -3,6 +3,7 @@
resources :users, :only => [:index, :show] do resources :users, :only => [:index, :show] do
resources :subdomains, :shallow => true resources :subdomains, :shallow => true
end end
match '/' => 'home#index', :constraints => { :subdomain => 'www' }
match '/' => 'sites#show', :constraints => { :subdomain => /.+/ } match '/' => 'sites#show', :constraints => { :subdomain => /.+/ }
root :to => "home#index" root :to => "home#index"


Expand Down
1 change: 1 addition & 0 deletions template.rb
Expand Up @@ -724,6 +724,7 @@ def limit_subdomain_access
resources :users, :only => [:index, :show] do resources :users, :only => [:index, :show] do
resources :subdomains, :shallow => true resources :subdomains, :shallow => true
end end
match '/' => 'home#index', :constraints => { :subdomain => 'www' }
match '/' => 'sites#show', :constraints => { :subdomain => /.+/ } match '/' => 'sites#show', :constraints => { :subdomain => /.+/ }
root :to => "home#index" root :to => "home#index"
RUBY RUBY
Expand Down

0 comments on commit c877fbb

Please sign in to comment.