nginx rewrite rules for html extensions

Jul 05, 2011

I Just swtiched to jekyll running in nginx. It's simple and lightning fast.

jekyll makes all output files .html however I wanted these files to be available to people without the explicit need to have .html in the url.

I found the following rewrite rule to solve exactly that.

location / {
  # break if URI has .html extension
  if ($request_filename ~* ^.+.html$) {
    break;
  }
  # add .html to URI and serve file, directory, or symlink if it exists
  if (-e $request_filename.html) {
    rewrite ^/(.*)$ /$1.html last;
    break;
  }
}