Right - once more unto the breach and all that! Thanks for this, I see
what you mean. I’ve part implemented, the bit I don’t quite get relates
to finding the relevant property and what I put in the routes file to
hook it up (hooking up the default root to a domain/property).
The bit you describe here…
“Next decide what your root path is going to be and set map.root
accordingly. You may find there is no need to go through the property
controller at all for the end-user since the whole domain is about a
single property and you define that outside in your seed data or admin
console.”
My complicated (perhaps over) routes.rb is like this at present.
http://pastie.org/1230080
Here’s what I have just done in the last hour or so all good as far as
it goes I think !
migrated a new Domain model
property belongs to domain
domain has one property
(all hooked up and playing nicely)
App controller…
before_filter :domain_lookup
private
def domain_lookup
@domain = Domain.find_by_host(request.host)
# raises errors when necessary
end
bit of my header file for testing…
Request host is :
<%= request.host %>
Set domain in app controller is : <%= @domain.host %>
Property that belongs to this domain is : <%= @domain.property.name %>
as you can se this test works…the find works nicely…
http://chaletcordee.co.uk/properties/1
http://apartmentnicole.co.uk/properties/2/
apache side…just as you suggested, understood nice and simple (I like
the www striping! no-www is interesting)
<VirtualHost *:80>
ServerName chaletcordee.co.uk
ServerAlias www.chaletcordee.co.uk apartmentnicole.co.uk
www.apartmentnicole.co.uk
then I rip off the www. using this rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.)$ [NC]
RewriteRule ^(.)$ http://%1 [R=301,L]
RailsEnv production
RailsBaseURI /
DocumentRoot /home/rupert/public_html/bookings/public
Stuck on next step!
Owain wrote in post #956838:
I have built something along these lines and here is how it might work
for you.
Have a domain model that maps a domain name to a property_id(decide
whether you are going to use www or no-www (http://no-www.org/) or
both if you want but it it is better to choose one or the other for
SEO and analytics.
so in your application_controller.rb put in some code that effectively
does a lookup on the domain model to find your property.
before_filter :domain_lookup
private
def domain_lookup
@domain = Domain.find_by_host(request.host)
raise DomainNotFound unless @domain # should always find it as
you defined it right?
raise DomainUnavailable unless @domain.live? # the on button for
the domain
raise DomainMaintenance if @domain.maintenance? # if you need to
pull it down
@host = @domain.host + request.port_string # I need this when
building some redirects as my setup is more complicated, SSL etc.
end
Now you have @domain.property available in your application wherever
you need it assuming you set up a belongs_to association. Now you can
refer to something like @domain.property.photos in your application.
I use @domain.css to pick up which of my CSS’s I want to use with that
domain. You can have as much as you like in there such as maintenance
flags etc.
Next decide what your root path is going to be and set map.root
accordingly. You may find there is no need to go through the property
controller at all for the end-user since the whole domain is about a
single property and you define that outside in your seed data or admin
console.
Now for the virtual host config in Apache.
<VirtualHost :80>
# define any of you domains as the servername, it
doesn’t matter which one you choose to be the main one (perhaps your
admin domain?)
ServerName property1.com
# list all of the other domains that run against the
same rails application remembering to pick up the www’s too and
wildcards work too!
ServerAlias
skichalets.com www.skichalets*.com
ahouse.com www.ahouse.com
myshed.com www.myshed.com
www.property1.com
# then I rip off the www. using this rule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1 [R=301,L]
RailsEnv production
RailsBaseURI /
DocumentRoot /srv/www/vhosts/yourapp/current/public
Warning if you are using SSL, you can only have one SSL host per IP
address. Basically the host name is not available to apache until it
has been decrypted by the host certificate, by which time it is too
late! I work around this by passing the property_id to the ssl
redirects and then use this to look up the domain. Just be careful
about rebuilding and building sessions.
So the logical flow goes like this:
user puts a URL into browser
apache picks up the host name and checks to see if it is the list of
hosts
rips the www off the host name if you want it to
send the request to be routes.rb in /srv/www/vhosts/yourapp/current/
config
your application_controlller.rb looks up the domain from request.host
HTH
O.