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





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

i.e. -> http://localhost:3000/properties/1/ ->
http://localhost:3000/properties/1/full_details -> http://localhost:3000/properties/2/


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

Anyone done anything like this before?

You could do this with Apache
Look at modrewrite


Luis S. wrote in post #954771:

You could do this with Apache
Look at modrewrite


Thanks that’s great.

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

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

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

So for example you’d have your site like this defined in your

<VirtualHost :80>
RewriteEngine on
RewriteRule ^(.
)$ http://localhost:3000/properties/1/$1 [R]

<VirtualHost :80>
RewriteEngine on
RewriteRule ^(.
)$ http://localhost:3000/properties/2/$1 [R]

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


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>
RewriteEngine on
RewriteRule ^(.
)$ APP_ROOT/properties/2/$1 [R]

I tried simply

<VirtualHost :80>
RewriteEngine on
RewriteRule ^(.
)$ /properties/2/$1 [R]

But that resulted in a mad loop of redirects !

as did…

<VirtualHost :80>
RewriteEngine on
RewriteRule ^(.
)$$1 [R]

Just to be clear (if it’s not already), in the end I’m trying to get the
resulting url to look like this.
with no mention of property in it.

Yes, it will

You’ll want to use wildcards


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


totally lost.

On Oct 18, 2:43pm, bingo bob [email protected] wrote:

<VirtualHost :80>
RewriteEngine on
RewriteRule ^(.
)$$1 [R]

Just to be clear (if it’s not already), in the end I’m trying to get the
resulting url to look like this.
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.


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

RewriteRule ^/$$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 bit, something like this… 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>



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]


So I feel a bit less lost now… try in a browser
appears to work and sub links are working too, e.g. full_details.

does that look right to you so far?

I’m half way there then?

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

Will this approach work?

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.
should be

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.
Fred? :slight_smile:

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


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

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


bingo bob wrote in post #955373:

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

Virtual host names can never be implemented completely within Rails,
since it’s up to Apache to dispatch to the correct server instance based
on the hostname – thus, the logic happens before Rails ever sees it.

Anything not involving the hostname can (and usually should) be done
with Rails routing.


Marnen Laibow-Koser
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs