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:
The following config allowed me to redirect /index.html
to /
and /subdir/index.html
to /subdir/
:
# Strip "index.html" (for canonicalization)
if ( $request_uri ~ "/index.html" ) {
rewrite ^(.*)/ $1/ permanent;
}
For the root /index.html
, the answer from Nicolas resulted in a redirect loop, so I had to search for other answers.
This question was asked on the nginx forums and the answer there worked better. http://forum.nginx.org/read.php?2,217899,217915
Use either
location = / {
try_files /index.html =404;
}
location = /index.html {
internal;
error_page 404 =301 $scheme://domain.com/;
}
or
location = / {
index index.html;
}
location = /index.html {
internal;
error_page 404 =301 $scheme://domain.com/;
}
The solutions quoting $scheme://domain.com/
assume that the domain is hard-coded. It was not in my case and so I used:
location / {
...
rewrite index.html $scheme://$http_host/ redirect;
... }
I use the following rewrite in the top level server clause:
rewrite ^(.*)/index.html$ $1 permanent;
Using this alone works for most URLs, like http://foo.com/bar/index.html
, but it breaks http://foo.com/index.html
. To resolve this, I have the following additional rule:
location = /index.html {
rewrite ^ / permanent;
try_files /index.html =404;
}
The =404
part returns a 404 error when the file is not found.
I have no idea why the first rewrite alone isn't sufficient.
This one works:
# redirect dumb search engines
location /index.html {
if ($request_uri = /index.html) {
rewrite ^ $scheme://$host? permanent;
}
}
For some reason most of the solutions mentioned here did not work. The ones that worked gave me errors with missing / in the url. This solution works for me.
Paste in your location directive.
if ( $request_uri ~ "/index.html" ) {
rewrite ^/(.*)/ /$1 permanent;
}