Skip to content
  • About
  • Friends
  • About
  • Friends
The Blog of Philippmy personal Site of Things
  • About
  • Friends
Written by Philipp on 2012-02-12

Running a Rails 3 Application in a Sub-URI Enviroment

Hacking . ruby . Ruby on Rails

Sometimes you need to run your Rails (3) Application on a Sub-URI (e.g. examle.com/prod, example.com/dev). In my current Project there was a Problem with that Configuration and the Rails url-helpers (link_for, url_for, usw.) becourse the app wasn’t aware of the necessary prefix (in our example “/dev”, “/prod”).

There is always the possibility to set the apps basic URL within your app-Context. Together with setting the prefix in a System Enviroment it was possible to achieve the wanted app behaviour.
How to configure a Rails3 App with Passenger in a Sub-URI Configuration please have a look in the Phusion Passenger users guide here or in another blog post here.

The Basic Apache Configuration looks somehow like this:

<VirtualHost *:80>
    ServerName      example.com
    ServerAdmin     admin@example.com
    DocumentRoot    /var/www
    # we added two file hardlinks from our apps public folder to /var/www/dev,  /var/www/prod
        <Directory /var/www/prod>
        ...
        SetEnv RAILS_RELATIVE_URL_ROOT /prod
        ...
    </Directory>
        <Directory /var/www/dev>
        ...
        SetEnv RAILS_RELATIVE_URL_ROOT /dev
        ...
    </Directory>
</VirtualHost>

The last part is to add a Before-Hook to the ApplicationController for updating the apps default_url if RAILS_RELATIVE_URL_ROOT is set:

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :action_set_url_options

  def action_set_url_options
    if ENV['RAILS_RELATIVE_URL_ROOT']
      @host = request.host+":"+request.port.to_s+"/"+ENV['RAILS_RELATIVE_URL_ROOT']
    else
      @host = request.host+":"+request.port.to_s
    end
    Rails.application.routes.default_url_options = { :host => @host}
  end
end

Share this:

  • Share on X (Opens in new window) X
  • Share on Facebook (Opens in new window) Facebook

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

Archives

  • August 2025
  • November 2023
  • February 2023
  • January 2023
  • April 2020
  • January 2018
  • December 2017
  • May 2017
  • February 2016
  • September 2015
  • December 2014
  • August 2014
  • June 2014
  • March 2014
  • February 2014
  • September 2013
  • August 2013
  • July 2013
  • November 2012
  • October 2012
  • September 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • January 2011
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • January 2010
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • September 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006

Calendar

February 2012
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
272829  
« Jan   Mar »

Categories

  • Bash
  • Bochum
  • Build
  • CCC
  • CLI
  • Coderwall
  • Coventry
  • DB
  • Edu
  • Freenas
  • Gitlab
  • Graphics
  • Hacking
  • iOS
  • Java
  • Javascript
  • Mac
  • NAS
  • Network
  • nexenta
  • Perl
  • Personal
  • PHP
  • Play! Framework
  • Proxmox
  • ruby
  • Ruby on Rails
  • Security
  • SmartOS
  • Snippets
  • Sound
  • Tech
  • Testing
  • Tooling
  • Twitter
  • UI
  • Uncategorized
  • Video
  • Virtualisierung
  • ZFS

Copyright The Blog of Philipp 2026 | Theme by ThemeinProgress | Proudly powered by WordPress

%d