在Nginx中将/foo.html重定向到/foo但不将/重定向到/index [英] redirect /foo.html to /foo but not / to /index in nginx
问题描述
我在磁盘上的文件具有扩展名: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
.
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
.
我的配置如下:
rewrite ^(/.+)\.html$ $scheme://$host$1 permanent;
location / {
root /var/www;
try_files $uri.html $uri $uri/ =404;
}
这确实将/a.html
重定向到/a
并成功从磁盘加载a.html
:
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
但是它将/
发送到/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
:
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
为什么会这样?我可以同时对两个我想要的东西(没有.html
扩展名,在URL中没有index
)都做nginx吗?
Why would this happen? Can I make nginx to both things I want (no .html
extension, no index
in url) at the same time?
推荐答案
我认为您的重写规则可能是向后的.也许就是这样(没有重写规则):
I think your rewrite rule might be backwards. Maybe simply this (no rewrite rule):
location / {
try_files $uri.html $uri $uri/ =404;
}
location = / {
index index.html;
}
编辑版本:
对不起,我没有完全理解您的描述.我重读了几次,并对其进行了测试,它可能接近您要执行的操作:
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:
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;
}
代码示例(修订版3):
CODE EXAMPLE (REVISION 3):
我希望第三次是魅力.也许这可以解决您的问题?
I'm hoping the third time is a charm. Maybe this will solve your problem?
# 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;
}
这篇关于在Nginx中将/foo.html重定向到/foo但不将/重定向到/index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!