multiple domain names mapping to different rails app URLs

OK, I'm really confused as to how this might work/if it's possible or even if I'm thinking about it right, so I'll just explain what I'd like to do and you can shoot holes in it.

I have an app running that is for properties. Each property has it's own "website" within my app and a user can tweak the content, it works well, quite pleased so far.

So the URLs to a property look like this for the root

http://localhost:3000/properties/1/

then

http://localhost:3000/properties/1/full_details http://localhost:3000/properties/1/price_list

etc.

Is it possible for different urls to point to different pages in someway.

i.e.

www.chalet-yeti.com -> http://localhost:3000/properties/1/

www.chalet-yeti.com/full_details -> http://localhost:3000/properties/1/full_details

www.apartment-marie.com -> http://localhost:3000/properties/2/

etc....

Is this possible/advisable/doable in the same rails app (I can't think how)? In case it matters the app runs under apache and passenger on my server.

Anyone done anything like this before?

You could do this with Apache Look at modrewrite

Luis

Luis Saffie wrote in post #954771:

You could do this with Apache Look at modrewrite

Luis

Thanks that's great.

Will it have the flexibility to deal with all the sub pages?

Yes, it will

You'll want to use wildcards

Luis

Thanks for this. I'm about to try and implement it, appreciate this is a little OT, but has anyone got any more tips for me or even a quick example.

Just to be clear, I'm using passenger - will that get in the way or also need any configuration?

Someone's suggested that actually using virtual hosts maybe a better way for me to do this? Thoughts?

Yeah, for this setup it's recommended to use virtual hosts along with modrewrite

So for example you'd have your site like this defined in your apache.conf

<VirtualHost *:80>   ServerName www.chalet-yeti.com   RewriteEngine on   RewriteRule ^(.*)$ http://localhost:3000/properties/1/$1 [R] </VirtualHost>

<VirtualHost *:80>   ServerName www.apartment-marie.com   RewriteEngine on   RewriteRule ^(.*)$ http://localhost:3000/properties/2/$1 [R] </VirtualHost>

I didn't test if this will work but if it doesn't, it should be pretty close. The idea is to have anything that goes to chalet-yeti.com go to http://localhost:3000/properties/1 and anything that follows that url, will be appended to the redirect.

If this is a bit too complicated, I suggest you do your homework and read about modrewrite some more. http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Luis

Hi Luis,

Thanks for this, I'm getting *really* close I think.

That looks fine and I've tried - the redirects are taking effect but it's not quite right.

The localhost:3000 part was just on my Mac, now I'm on the server I need to replace that in some way with the root of the rails app - how do I refer to it in a virtualhost tag.

Something like this I imagine (obviously not right)...

<VirtualHost *:80>   ServerName www.apartment-marie.com   RewriteEngine on   RewriteRule ^(.*)$ APP_ROOT/properties/2/$1 [R] </VirtualHost>

I tried simply

<VirtualHost *:80>   ServerName www.apartment-marie.com   RewriteEngine on   RewriteRule ^(.*)$ /properties/2/$1 [R] </VirtualHost>

But that resulted in a mad loop of redirects !

as did...

<VirtualHost *:80>   ServerName www.apartment-marie.com   RewriteEngine on   RewriteRule ^(.*)$ apartment-marie.com/properties/2/$1 [R] </VirtualHost>

Just to be clear (if it's not already), in the end I'm trying to get the resulting url to look like this. http://www.apartment-marie.com/ and http://www.apartment-marie.com/full_details with no mention of property in it.

Hi Luis,

Thanks for this, I'm getting *really* close I think.

That looks fine and I've tried - the redirects are taking effect but it's not quite right.

The localhost:3000 part was just on my Mac, now I'm on the server I need to replace that in some way with the root of the rails app - how do I refer to it in a virtualhost tag.

Something like this I imagine (obviously not right)...

<VirtualHost *:80> ServerNamewww.apartment-marie.com RewriteEngine on RewriteRule ^(.*)$ APP_ROOT/properties/2/$1 [R] </VirtualHost>

I tried simply

<VirtualHost *:80> ServerNamewww.apartment-marie.com RewriteEngine on RewriteRule ^(.*)$ /properties/2/$1 [R] </VirtualHost>

But that resulted in a mad loop of redirects !

as did...

<VirtualHost *:80> ServerNamewww.apartment-marie.com RewriteEngine on RewriteRule ^(.*)$ apartment-marie.com/properties/2/$1 [R] </VirtualHost>

Just to be clear (if it's not already), in the end I'm trying to get the resulting url to look like this.http://www.apartment-marie.com/ andhttp://www.apartment-marie.com/full_details with no mention of property in it.

Sounds like you only want to redirect someone touching the root of the site, ie rewrite ^/$ not ^.*$. If you want there to be no trace of the property id in the url then you'll need to do something rails side, probably inspect the host name in the rails and do something with that.

Fred

As Fred metioned. Looks like there's two rewrites here. Only needs to be done from the Apache side and the other from rails (routes.rb) I suggest you get the apache site going before attempting the other rewrite.

Luis

totally lost.

Ok, let's back up a bit.

For now just do the apache rewrite as I mentioned above. I think Fred is right that you need to use ^/$ rather than ^.*$ so your rule would look like

RewriteRule ^/$ apartment-marie.com/properties/2/$1 [R]

I think that's it.

Thanks for bearing with me.

That one didn't work - close but it gave me a double rendering of the apartment-marie.com bit, something like this... apartment-marie.com/apartment-marie.com/properties bla... so I edited like so (here's the real file and it's live if it helps)..

This is much better (ignore the silly google api key message, will sort in time)

<VirtualHost *:80>

  ServerName chaletcordee.co.uk   # ServerAlias www.chaletcordee.co.uk

  DocumentRoot /home/rupert/public_html/bookings/public

  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css   AddOutputFilterByType DEFLATE application/javascript   AddOutputFilterByType DEFLATE application/x-javascript

  Options +FollowSymLinks   RewriteEngine on   RewriteRule ^/$ /properties/1/$1 [R]

</VirtualHost>

So taking it a step on I figure I need to work on a few things...

need to make sure www versions all operate ok.

need to make sure that when I add another domain, that works to.

http://chaletcordee.co.uk/properties/1/comments should be http://chaletcordee.co.uk/comments

guess this is the rails part. My routes file is here, not bothered if the nested admin routes need to be acessesed with an ugly URL for now, it's just the public facing pages that'd be important initially.

pastie of my routes file. http://pastie.org/1230080 Fred? :slight_smile:

I'm half way there then?

Need to handle the rest on the rails side - via my routes.rb file?

Will this approach work?

yes, it should work. Look like you may need to add single resources along with the nested ones. Not entirely sure if you can do this though...

Luis

Anyone else comment as to if this combination of apache rewrites and rails routing will play nicely together. I'll try it of course just imagine that it may have been done before by somebody.

You could do it all with modrewrite. You'll just need to specify the specific rules and rewrite actions for all your cases. It's not that hard, just read up on it.

There are countless of resources on the web about this. Here's a starting point http://www.webmastertips.us/tutorials/mod_rewrite.htm

I was thinking of writing an article about this on my site but there's so much out there that it seems pointless.

Luis.

Thanks Luis, I'll refer to them and see if I can find a solution totally via apache/modrewrite; I suspect I can! I need to get a handle on the syntax of those rewrite commands, the command chain and regexes, should be ok. I'll post back to this thread with the solution when I crack it.

You need to wear *many* hats in this game at times, not every problem seems to be solvable in the RoR app itself (or perhaps it is but the *pragmatic* approach for a quick win is to ssh to the server and get funky with with apache config).