【发布时间】:2012-12-07 08:41:55
【问题描述】:
我在磁盘上的文件有扩展名: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 重定向到/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
为什么会发生这种情况?我可以同时将 nginx 用于我想要的两个东西(没有 .html 扩展名,在 url 中没有 index)吗?
【问题讨论】: