Rails plugin: relative_path
So you’re developing a rails app, happily using WEBrick to test everything out, and now it’s time to deploy to another server. Lots of folks seem to be going the webserver->rails server/cluster route with LightHTTP or Apache, etc. For me, I typically set up virtual directories in Apache and use mod_proxy to tie it all together.
What’s the problem? Well, let’s say you have a proxy’d directory called /railsapp. You probably have a multitude of lines in your app that generate links, ala:
Now, if you’re running your rails app within a proxy’d virtual directory, you’ll get:
http://some.host.com/Controller/show
instead of:
http://some.host.com/railsapp/Controller/show
Enter the Relative Path Plugin. As documented, one line in your controllers (or just once per app in ApplicationController) and all your link_to links are back to working perfectly.
Comments
3 Responses to “Rails plugin: relative_path”
Leave a Reply

Hey, do you know how to fix this plugin to run on rails 2.0.x? thanks!
It looks like it is broken in Rails 2.x (http://wiki.rubyonrails.org/rails/pages/Relative+Path+Plugin). I haven’t deployed a Rails 2 app behind Apache since Rails 2, so I haven’t had to consider a workaround.
If I do end up needing this plugin soon, I’ll take a look.
BTW, if using mongrel as the webserver, then all you need is
mongrel_rails start -p 3000 –prefix /railsapp
to get a similar effect. If using mongrel_cluster, then set “prefix: /railsapp” in the yaml config file.
This is arguably a bit more robust than relative URLs, since even redirects will have a valid absolute URL, and it needs no plugin.
Your mod_proxy config needs to leave /railsapp in the path as it is proxied, since Rails will expect to see it there when parsing the path (it will return NOT FOUND if it is not)
This works by setting ActionController::AbstractRequest.relative_url_root
and under web servers other than mongrel you may be able to get this effect by setting RAILS_RELATIVE_URL_ROOT in the *request* environment (e.g. using SetEnv with fastcgi)