Skip to content

Commit

Permalink
Allow running Enki under a URL path prefix
Browse files Browse the repository at this point in the history
I run my blog in a subsdirectory of my web server.  That is,
instead of blog.example.com, I use example.com/blog.  Rails 3 no longer
supports that as an out-of-the-box configuration option.

(There is an action_controller.relative_url_root key, but it doesn't
handle asset helper paths or, under some servers, the top-level route
rewriting.)

Tweak routing and URLs in several places:

* config.ru -- addresses most routing
* Set RAILS_RELATIVE_URL_ROOT to make asset helpers work
* Fix Enki-specific URLs in post_path helper and admin view to work
  in a non-root-path deployment

In config/application.rb, comment an example line that sets
RAILS_RELATIVE_URL_ROOT.  The line is commented so that the app still
runs in "/", but a one-line config change or setting that environment
variable would change that.
  • Loading branch information
Marcel M. Cary committed Apr 16, 2013
1 parent a43d13c commit 925cf43
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/helpers/url_helper.rb
Expand Up @@ -2,6 +2,7 @@ module UrlHelper
def post_path(post, options = {})
suffix = options[:anchor] ? "##{options[:anchor]}" : ""
path = post.published_at.strftime("/%Y/%m/%d/") + post.slug + suffix
path = "#{ENV['RAILS_RELATIVE_URL_ROOT']}#{path}"
path = URI.join(enki_config[:url], path) if options[:only_path] == false
path
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/admin.html.erb
Expand Up @@ -36,7 +36,7 @@
<li><%= nav_link_to("Health", admin_health_path, :accesskey => '6') %></li>
<li class="separator">&nbsp;</li>
<li><%= link_to("Logout", admin_session_path, :accesskey => 'l', :method => :delete) %></li>
<li><%= link_to("View Site", '/', :accesskey => 'v', :title => 'View Site') %></li>
<li><%= link_to("View Site", root_path, :accesskey => 'v', :title => 'View Site') %></li>
</ul>
</div>
</div>
Expand Down
5 changes: 4 additions & 1 deletion config.ru
@@ -1,4 +1,7 @@
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
run Enki::Application

map ENV['RAILS_RELATIVE_URL_ROOT'] || "/" do
run Enki::Application
end
5 changes: 5 additions & 0 deletions config/application.rb
Expand Up @@ -20,6 +20,11 @@
# Bundler.require(:default, :assets, Rails.env)
end

# This configures the base path of routes for the main application.
# For example, set to '/blog' to run at http://example.com/blog
# It must appear before the Application class body. Initializers run too late.
#ENV['RAILS_RELATIVE_URL_ROOT'] = '/blog'

module Enki
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
Expand Down

0 comments on commit 925cf43

Please sign in to comment.