Hi, do you have any hint on how give to users the possibility to set an own domain instead of using a subdomain of the app? The best example i've found is how shopify works, they permit to have the shop under shopname.myshopify.com or set an own domain like shopname.com. I don't think they do this manually setting apache vhosts everytime.. The A record of the own domain is the to the myshopify.com, but do you have hany idea on how manage this situation? Thank you
You can setup wildcard dns (so anything.example.com will point to the right server) and similarly you can have a wildcard virtual host in apache.
Fred
You need to set a wildcard A-record, change the vhost config to serve the wildcard, then in a before_filter in your rails app handle the subdomain. Quite easy actually.
DNS:
add *.mydomain.com as A-record and point it to same IP as the mydomain.com one (if your host will allow it)
Apache Vhost config:
ServerAlias *.mydomain.com
…
Rails app (you could probably use account_location plugin to make it easier on yourself):
class ApplicationController < ActionController::Base
before_filter :check_subdomain
def check_subdomain
if !account_subdomain
render :file => “#{RAILS_ROOT}/public/404.html”, :layout => false, :status => 404 and return false
end
@current_site = Account.find_by_subdomain(account_subdomain.downcase)
if !@current_site
render :file => “#{RAILS_ROOT}/public/404.html”, :layout => false, :status => 404 and return false
end
end
end
This is just a quick and dirty draft, you should adapt it to your liking.
Best regards
Peter De Berdt
@Frederick: thank you, i know about the wildcard record to catch all the requests and give them to a default application. do you know if it's possible to have a structure like this?
maindomain.com first.users.maindomain.com second.users.maindomain.com userdomain.com -> third.users.maindomain.com
All run the same rails application, but every user have their own subdomain (i can't use a third level like first.maindomain.com because it would be a chaos having other subdomains), and they can have an own domain to point to their subdomain. The wildcard in this case will point to the application, but how tell apache and the rails app that the domain requested is for a given subdomain? Does peter's approach work for external domains too? (obviously the vhost would be both *.users.maindomain.com and * to chatch all external requests and redirect to the right subdomain)
@Peter: thank you too, i'll look into account_location.. i think that
with subdomains won't be a big problem. instead i'm more worried about
user's own domains
@Frederick: thank you, i know about the wildcard record to catch all the requests and give them to a default application. do you know if it's possible to have a structure like this?
maindomain.com first.users.maindomain.com second.users.maindomain.com userdomain.com -> third.users.maindomain.com
All run the same rails application, but every user have their own subdomain (i can't use a third level like first.maindomain.com because it would be a chaos having other subdomains), and they can have an own domain to point to their subdomain. The wildcard in this case will point to the application, but how tell apache and the rails app that the domain requested is for a given subdomain? Does peter's approach work for external domains too? (obviously the vhost would be both *.users.maindomain.com and * to chatch all external requests and redirect to the right subdomain)
I imagine that would work fine. the existing plugins are probably
tailored to just checking the subdomain but the principle is the same.
Fred
What Frederick said. If you read the account_location code, you’ll see it’s so easy you could easily implement it yourself. All it does is split out the full request domain and put it in easy accessors.
However, I’m really wondering if working with domain names like http//frederick.fredericks-long-company-name.my-nifty-new-webapplication.com/ is a good way to go or a really bad one.
Best regards
Peter De Berdt
you don't need a entire plugin for this. I use the recipe from Advanced Rails Recipes (http://railsbookclub.com/advanced-rails-recipes) It ads an extra column to your account model and uses a simple before filter to match the account to the subdomain.
True, but plugins do provide portability. If you plan on using this type of scoping a lot, a plugin that mixes in the functionality into any Rails app might be more feasible. It’s not like there’s a lot of complicated things going on in the plugin either. However, I do agree that it’s best to understand what the plugin actually does, so you can tailor (and fork) it to your own needs.
Best regards
Peter De Berdt