0

Existing functionality: try_files along with rewrite are being used to allow requests for foo.html and foo/ to redirect to foo.

Problem: If I specify try_files $uri.html $uri @noextension =404;, then a request to domain.com/ redirects to a 404 page but a request to domain.com/index works.

Problem 2: If try_files $uri.html $uri @noextension /index.html is used instead, the site root will return but requests for non-existent pages will also redirect to the site root instead of a 404 error.

Problem 3: The following also results in requests to / failing but /index working:

location / {
    try_files $uri @noextension;
}

# redirect /foo/ to /foo
rewrite ^(/.+)/$ $1 permanent;
# redirect /foo.html to /foo
rewrite ^(/.+)\.html$ $1 permanent;

location ~ \.html$ {
    try_files $uri =404;
}

All of the other site pages are fine, only requests to the site root are affected. How might I go about getting the index and 404 pages to correctly respond?

server {
    listen       80;
    server_name  localhost;

    root   /usr/share/nginx/html;
    index  index.html;

    location / {
        try_files $uri.html $uri @noextension =404;
    }

    # redirect /foo/ to /foo
    rewrite ^(/.+)/$ $1 permanent;
    # redirect /foo.html to /foo
    rewrite ^(/.+)\.html$ $1 permanent;

    location @noextension {
        # allow for domain.com rather then domain.com/index to respond
        rewrite ^(.*)$ $1.html last;
    }

    error_page  404              /404;
Astron
  • 377
  • 4
  • 16
  • This is some kind of nonsense. try_files $uri is useless - it's the default behaviour. It's impossible to understand from your config and from your notes what are you actually intended to do. – drookie Jan 14 '15 at 05:48
  • I have hopefully clarified the question. – Astron Jan 14 '15 at 15:19

0 Answers0