NGINX redirection issue

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


Hello!

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. 
> http://www.aaa.com/bbb/ccc.html should show the content of
> http://www.aaa.com/bbb/ccc.html but the URL should show
> http://www.aaa.com/bbb/ccc
> 
> 2. http://www.aaa.com/index.html should show the content of
> http://www.aaa.com/index.html but the URL should show http://www.aaa.com.

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:

http://nginx.org/r/index
http://nginx.org/r/try_files
http://nginx.org/en/docs/http/request_processing.html

> There are few rewrites already there which are given below
> 
> 1. Whenever you try to access the http://www.aaa.com site, it will redirect
> to http://www.aaa.com/aaa/bbb/ccc/ddd/index.html.
>  http://www.aaa.com    -----> http://www.aaa.com/aaa/bbb/ccc/ddd/index.html.
> 
> 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: 1.1.1.1,
> server: www.aaa.com, request: "GET /aaa/ccc HTTP/1.1", host: "www.aaa.com"
> 
> 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
http://nginx.org/



More information about the nginx mailing list