Nginx: rewrite rule to remove /index.html from $ request_uri

I saw several ways to rewrite $request_uri and add index.html to it when this particular file exists on the file system, for example:

 if (-f $request_filename/index.html) { rewrite (.*) $1/index.html break; } 

but I was wondering if the opposite is possible:

i.e. when someone requests http://example.com/index.html , they are redirected to http://example.com

Since nginx regexp is compatible with perl, I tried something like this:

 if ( $request_uri ~* "index\.html$" ) { set $new_uri $request_uri ~* s/index\.html// rewrite $1 permanent; } 

but this was mostly a hunch, is there any good documentation describing modrewrite for nginx?

+5
source share
7 answers

I use the following correspondence in a top-level server offer:

 rewrite ^(.*)/index.html$ $1 permanent; 

Using this only works for most URLs, for example http://foo.com/bar/index.html , but it breaks down http://foo.com/index.html . To solve this problem, I have the following additional rule:

 location = /index.html { rewrite ^ / permanent; try_files /index.html =404; } 

Part =404 returns error 404 when the file is not found.

I have no idea why the first rewrite is not enough.

+7
source

The following configuration 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; } 
+2
source

For the root /index.html answer from Nicholas led to a redirect loop, so I had to look for other answers.

This question was asked on the nginx forums, and the answer worked better there. http://forum.nginx.org/read.php?2,217899,217915

Use

 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/; } 
+1
source

This one works:

 # redirect dumb search engines location /index.html { if ($request_uri = /index.html) { rewrite ^ $scheme://$host? permanent; } } 
+1
source

For some reason, most of the solutions mentioned here did not help. Those that worked gave me missing / URL errors. This solution works for me.

Insert a location in your directive.

 if ( $request_uri ~ "/index.html" ) { rewrite ^/(.*)/ /$1 permanent; } 
+1
source

This works for me:

 rewrite ^(|/(.*))/index\.html$ /$2 permanent; 

It covers both the root instance of /index.html and the lower instances of /bar/index.html

The first part of the regular expression is basically converted as: [nothing] or /[something] - in the first case $ 2 is an empty string, so you redirect only to / , in the second case $ 2 is [something] , so you redirect to /[something]

Actually, I was a bit involved in covering index.html , index.htm and index.php

 rewrite ^(|/(.*))/index\.(html?|php)$ /$2 permanent; 
+1
source

Solutions citing $scheme://domain.com/ assume that the domain is hard-coded. This was not in my case, and therefore I used:

 location / { ... rewrite index.html $scheme://$http_host/ redirect; ... } 
0
source

All Articles