我在磁盘上的文件有扩展名:index.html, a.html. 我想要一个http://example.com/a加载/var/www/a.html和http://example.com/加载的请求/var/www/index.html.我希望任何其他网址重定向到规范网址,因此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;
}
Run Code Online (Sandbox Code Playgroud)
这不重定向/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
Run Code Online (Sandbox Code Playgroud)
但它发送/给/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
Run Code Online (Sandbox Code Playgroud)
如果我删除重写规则它会停止重定向/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
Run Code Online (Sandbox Code Playgroud)
为什么会这样?我可以同时为我想要的东西(没有.html扩展名,没有index网址)制作nginx 吗?
我认为你的重写规则可能是倒退的。也许只是这样(没有重写规则):
location / {
try_files $uri.html $uri $uri/ =404;
}
location = / {
index index.html;
}
Run Code Online (Sandbox Code Playgroud)
编辑版本:
抱歉,我没有完全理解你的描述。我重读了几次并对此进行了测试,它可能接近您想要做的事情:
location = / {
try_files /index.html =404;
}
location = /index {
return 301 $scheme://$host;
}
location ~* \.html$ {
rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
}
location / {
try_files $uri.html $uri/ @backend;
}
location @backend {
# rewrite or do whatever is default for your setup
rewrite ^ /index.html last;
// or return 404;
}
Run Code Online (Sandbox Code Playgroud)
代码示例(修订版 3):
我希望第三次是一次魅力。也许这能解决您的问题?
# example.com/index gets redirected to example.com/
location ~* ^(.*)/index$ {
return 301 $scheme://$host$1/;
}
# example.com/foo/ loads example.com/foo/index.html
location ~* ^(.*)/$ {
try_files $1/index.html @backend;
}
# example.com/a.html gets redirected to example.com/a
location ~* \.html$ {
rewrite ^(.+)\.html$ $scheme://$host$1 permanent;
}
# anything else not processed by the above rules:
# * example.com/a will load example.com/a.html
# * or if that fails, example.com/a/index.html
location / {
try_files $uri.html $uri/index.html @backend;
}
# default handler
# * return error or redirect to base index.html page, etc.
location @backend {
return 404;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2215 次 |
| 最近记录: |