How to configure Sub-folders with Ruby on Rails on Feather

Ruby on Rails configuration setup to host your blog as a /blog sub-folder

Published on:

How to configure Sub-folders with Ruby on Rails on Feather
Do not index
Do not index
Let’s say your main domain is www.mydomain.com and feather blog url is my-domain.feather.blog and you want to host on /blog sub-folder.
To configure the subfolder, we will need to setup a reverse proxy in your rails project. To do we will be using gem to install rack-reverse-proxy in your project.
In the config.ru or application route file add the following code:
reverse_proxy(%r{^/blog(.*)?$}, 'https://my-domain.feather.blog/blog$1', { preserve_host: true })
reverse_proxy(%r{^/_feather(.*)?$}, 'https://my-domain.feather.blog/_feather$1', { preserve_host: true })
Make sure to my-domain.feather.blog to your feather blog url and /blog to which ever route you choose to use.
 
Below is an example file that can help:
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('config/environment', __dir__)
require 'rack/proxy'

class ReverseProxyApp < Rack::Proxy
  def initialize(app)
    super(streaming: false)
    @app = app
  end

  def call(env)
    request_path = env["PATH_INFO"]
    
    if request_path.start_with?('/blog') || request_path.start_with?('/_feather')

      env["HTTP_X_ORIGINAL_HOST"] = original_host
      
      env["HTTP_HOST"] = "my-domain.feather.blog"
      
      response = super(env)
      
      status, headers, body = response
      
      if [301, 302, 303, 307, 308].include?(status) && headers['Location']
        location = headers['Location']
        
        if location.include?('my-domain.feather.blog')
          uri = URI(location)
          path = uri.path
          query = uri.query ? "?#{uri.query}" : ""
          
          headers['Location'] = "#{path}#{query}"
        end
      end
      
      [status, headers, body]
    else
      @app.call(env)
    end
  end
  
  def rewrite_env(env)
    request_path = env["PATH_INFO"]
    
    if request_path.start_with?('/blog') || request_path.start_with?('/_feather')
      env["REQUEST_URI"] = "https://my-domain.feather.blog#{request_path}"
    end
    
    env
  end
end

use ReverseProxyApp

run Rails.application

Once this is done, go to Feather. Navigate to Settings > Features and switch on “Enable Subdirectory” feature and after that go to Settings > Sub-folder management and enter the following values,
notion image
💡
Make sure to replace www.mydomain.com with the domain you are using and blog with whatever sub-route you plan to use.
Once you do these, your sub-folder should be live. You should be able to access your feather site on www.mydomain.com/blog. If it’s not reach out to us at support@feather.so.
 
Important Note: Under the Domains section make sure that you are using Subdomain settings. If you have added a custom domain there, please remove it. Sub-folders does not require the Custom Domain configurations and if you have it, then the sub-folder will not work.