My files on disk have extensions: index.html
, a.html
. I want a request for http://example.com/a
to load /var/www/a.html
and http://example.com/
to load /var/www/index.html
. I want any other url to redirect to a canonical url, so http://example.com/a.html
should redirect to http://example.com/a
.
My configuration looks like:
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
location / {
root /var/www;
try_files $uri.html $uri $uri/ =404;
}
This does redirect /a.html
to /a
and succeed at loading a.html
from disk:
$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location
Location: http://www.jefftk.com/food
$ curl -s http://www.jefftk.com/food | grep ^Location
But it sends /
to /index
:
$ curl -s -D- http://www.jefftk.com/pictures/ | grep ^Location
Location: http://www.jefftk.com/pictures/index
$ curl -s -D- http://www.jefftk.com | grep ^Location
Location: http://www.jefftk.com/index
If I remove the rewrite rule it stops redirecting from /a.html
to /a
but also stops sending /
to /index
:
$ curl -D- -s http://www.jefftk.com/food.html | grep ^Location
$ curl -D- -s http://www.jefftk.com/food | grep ^Location
$ curl -D- -s http://www.jefftk.com/ | grep ^Location
$ curl -D- -s http://www.jefftk.com/pictures/ | grep ^Location
Why would this happen? Can I make nginx to both things I want (no .html
extension, no index
in url) at the same time?
Are you looking for something like this:
Basically this will try the
a.html
file first, if that fails it will then tryindex.html
and last show a404
. Also please remember torestart nginx
after you edit yourvhost
file.I think your rewrite rule might be backwards. Maybe simply this (no rewrite rule):
EDITED VERSION:
Sorry, I wasn't understanding your description completely. I reread it several times and tested this and it might be closing to what you are looking to do:
CODE EXAMPLE (REVISION 3):
I'm hoping the third time is a charm. Maybe this will solve your problem?