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;
0 Answers