NGINX redirection issue

Maxim Dounin mdounin at
Thu Aug 7 08:03:59 UTC 2014


On Wed, Aug 06, 2014 at 11:37:42PM -0400, manish-ezest wrote:

> Hello All,
> I am facing some issue regarding nginx redirection. I want to remove .html
> extension from all the html pages and all the index.html pages. For example
> 1. 
> should show the content of
> but the URL should show
> 2. should show the content of
> but the URL should show

This is something as simple as:

    location / {
        index index.html;
        try_files $uri.html $uri/ =404;

with "index index.html" being the default - that is, you don't 
actually need to configure it explicitly.  Or like this, using 
only try_files instead of try_files + index:

    location / {
        try_files $uri.html $uri/index.html =404;

The only potential difference from what you describe is that this 
won't do a redirect to hide "/index.html" if explicitly requested 
by a user, but it's not clear from your description if it's 
something really needed.  This is not something usually done.

See here for details:

> There are few rewrites already there which are given below
> 1. Whenever you try to access the site, it will redirect
> to
>    ----->
> 2. All directory lookups to 'index.html'
> rewrite ^(.*)/$ $1/index.html permanent;
> 3. All open strings to index.html
> rewrite ^(.*)/([^\.\?/]+)$  $1/$2/index.html permanent;
> Now whenever I try to implement the change, the site went into a redirect
> loop and gives following error in the error log. 
> 2014/08/05 14:48:03 [error] 10392#0: *750 rewrite or internal redirection
> cycle while internally redirecting to "/index.html", client:,
> server:, request: "GET /aaa/ccc HTTP/1.1", host: ""
> Please suggest how to solve the issue

Both rewrites you've provided will return external 301 redirects 
without doing internal redirects, so the loop is caused by 
something else in your config.  You have to show your config for 
others to be able to help.

Maxim Dounin

More information about the nginx mailing list