Last Updated: February 25, 2016
·
1.336K
· brunopazzim

Sinatra + Octopress on a subdirectory

This will be helpful if you want your blog to run on a subdirectory of a Sinatra app.

After generating your Octopress blog, following the steps to deploy to a subdirectory, put the generated folder in the /public folder of your Sinatra app. Then edit your site.rb file and add these lines:

get(/.+/) do
  send_sinatra_file(request.path) {404}
end

not_found do
  send_file(File.join(File.dirname(__FILE__), 'public', '404.html'), {:status => 404})
end

def send_sinatra_file(path, &missing_file_block)
  file_path = File.join(File.dirname(__FILE__), 'public',  path)
  file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i
  File.exist?(file_path) ? send_file(file_path) : missing_file_block.call
end 

After that, your blog should be accessible when you start the server, on localhost:4567/yoursubdirectoryname

I've create a small app to demonstrate it working. You can access it on GitHub here.