我在磁盘上的文件有扩展名:index.html
, a.html
. http://example.com/a
我想要一个加载/var/www/a.html
和http://example.com/
加载的请求/var/www/index.html
。我希望任何其他 url 重定向到规范 url,所以http://example.com/a.html
应该重定向到http://example.com/a
.
我的配置如下:
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
location / {
root /var/www;
try_files $uri.html $uri $uri/ =404;
}
这确实重定向/a.html
到并成功从磁盘/a
加载:a.html
$ 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
但它发送/
到/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
如果我删除重写规则,它会停止从/a.html
to重定向,/a
但也会停止发送/
到/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
为什么会发生这种情况?我可以同时为我想要的两个东西(没有.html
扩展名,没有index
url)制作 nginx 吗?