Optional Html – an alternative to Directory Indexes for pretty URLs

The Rack::OptionalHtml middleware handles extensionless pages on a Middleman server

If there is no match for the url, it will try the same url with some different versions of a .html suffix.

The effect is similar to using the Directory Index extension, but is less intrusive in that it will not introduce extra directories, rename the actual page to index.html, etc. It also makes it possible to call images by filename only.

On the down side, it will only effect the Middleman development server, so the production server needs to be configured to behave in the same way.

The gem can be found here: https://github.com/tommysundstrom/middleman-rack-optional-html

I can’t get the gem to install I get this error:

Could not find gem 'optional_html (>= 0) ruby' in the gems available on this machine.

Looks like your Middleman has problem finding the gem.

I’m new to writing gems, so probably I’ve made a misstake somewhere. Work is a bit intense right now, but I’ll look into it as soon as I can. But here are some things you could try:

  • Remove the require "rack/middleman/optional_html" line from `config.rb``

  • Delete your Gemfil.lock and run bundle installagain. If you do gem list, is optional-html in the list?

What’s the advantage over rack-contrib’s try-static ?

require 'rack/contrib/try_static'
use Rack::TryStatic,
  root: 'build',
  urls: %[/],
  try: %w[.html index.html /index.html]

I started by trying to use try-static, but could not get it to work, so optional_html is a modification of it that works with (my) Middleman (in development mode). If Try Static works for you, it’s preferable to use it. Also, if you serve your site from a Rack server, you should use Try Static there.

Your example above looks like it has the root in the built site. Have you got it to work in development mode as well?

iencrypt, add

gem "optional_html", :git => 'https://github.com/tommysundstrom/middleman-rack-optional-html.git'

to the Gemfile, and then run bundle install.

I’ve updated the installation instructions, and also made the relation to Try Static clearer.

tommysundstrom I wasn’t emailed that you had posted I apologize for the delay. I’ve just updated the gem file and I’m going to test it out right now. Thanks so much!

Hey guys I just wanted to post here the nginx line that I’m currently using to allow you to run the site without the .html extension on the production server just in case anyone needs it.

location / { try_files $uri.html $uri/index.html /fallback.html; }
location = /fallback.html { }