I’ve seen a few ways to rewrite the $request_uri and add the index.html to it when that particular file exists in the file system, like so:
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
but i was wondering if the opposite is achievable:
i.e. when somebody requests http://example.com/index.html, they’re redirected to http://example.com
Because the nginx regexp is perl compatible, i tried something like this:
if ( $request_uri ~* "index\.html$" ) {
set $new_uri $request_uri ~* s/index\.html//
rewrite $1 permanent;
}
but it was mostly a guesswork, is there any good documentation describing the modrewrite for nginx ?
I use the following rewrite in the top level server clause:
Using this alone works for most URLs, like
http://example.com/bar/index.html, but it breakshttp://example.com/index.html. To resolve this, I have the following additional rule:The
=404part returns a 404 error when the file is not found.I have no idea why the first rewrite alone isn’t sufficient.