How to remove .html from URLs with nginx rewrites

Shell Script (Bash) posted over 2 years ago by christian

This will rewrite http://xxx.com/porn to http://xxx.com/porn.html for added pleasure:

   1  location / {
   2          # break if URI has .html extension
   3          if ($request_filename ~* ^.+.html$) {
   4            break;
   5          }
   6          # add .html to URI and serve file, directory, or symlink if it exists
   7          if (-e $request_filename.html) {
   8            rewrite ^/(.*)$ /$1.html last;
   9            break;
  10          }
  11        }

This URL will also work http://xxx.com/porn/

Tagged nginx, html, rewrite