Forum: Ruby on Rails Give own domains to users (shopify like)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mix M. (Guest)
on 2009-03-02 15:46
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-03-02 15:57
(Received via mailing list)
On Mar 2, 1:12 pm, fausto <removed_email_address@domain.invalid> wrote:
> 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?

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
Peter De Berdt (Guest)
on 2009-03-02 15:58
(Received via mailing list)
On 02 Mar 2009, at 14:12, fausto wrote:

> 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?

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
Mix M. (Guest)
on 2009-03-03 01:43
(Received via mailing list)
@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 C. (Guest)
on 2009-03-03 02:03
(Received via mailing list)
On 2 Mar 2009, at 23:42, fausto wrote:

> 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
Peter De Berdt (Guest)
on 2009-03-03 10:59
(Received via mailing list)
On 03 Mar 2009, at 01:02, Frederick C. wrote:

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

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
Vincent B. (Guest)
on 2009-03-03 12:40
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.
Peter De Berdt (Guest)
on 2009-03-03 12:56
(Received via mailing list)
On 03 Mar 2009, at 11:40, Vincent B. wrote:

> 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
This topic is locked and can not be replied to.