It’s not particularly hard to deploy your app in a subdirectory, but I’ve found that figuring out how to can be. So, I decided to put together this short little write-up.
It turns out Mongrel versions 0.3.14.x and up have a —prefix command line parameter for locating your Rails app at an arbitrary URL. Of course, you should already be running version 1.0 anyways. Here’s a single production Mongrel instance running on port 8000, prefixed with ‘subdir’:
mongrel_rails start -e production -p 8000 --prefix=/subdir
Your app should now be accessible at http://localhost:8000/subdir.
How does this work? Aside from prefixing all your routes with the ‘/subdir’ path, the —prefix option also modifies the result of all url_for (and thus link_to) method calls to include the new path. That means that, given a religious use of the link_to method, you can arbitrarily relocate your program anywhere at the drop of a hat. Not bad.
url_for :controller=> 'admin', :action => 'login' # => '/subdir/admin/login'
I’m going to assume you’ve already figured out how to proxy Mongrel through Apache. As far as your subdirectory goes, you need the following two lines in your Apache conf file:
ProxyPass /subdir http://localhost:8000/subdir ProxyPassReverse /subdir http://localhost:8000/subdir
The “tricky” part here is to include the subdirectory in both the source and destination URLs of your proxy commands.
Anyways, that’s it. Launch your Mongrel (cluster), restart Apache, and you’re good to go.