gpt4 book ai didi

$ request_uri中的nginx : rewrite rule to remove/index. html

转载 作者:行者123 更新时间:2023-12-04 13:24:41 27 4
gpt4 key购买 nike

当文件系统中存在该特定文件时,我已经看到了几种重写$request_uri并将index.html添加到其中的方法,例如:

if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}

但我想知道相反的情况是否可以实现:

即当有人请求 http://example.com/index.html时,他们被重定向到 http://example.com
因为nginx regexp是与perl兼容的,所以我尝试了如下操作:
if ( $request_uri ~* "index\.html$" ) {
set $new_uri $request_uri ~* s/index\.html//
rewrite $1 permanent;
}

但这主要是一个猜测,是否有任何好的文档描述nginx的modrewrite?

最佳答案

我在顶级服务器子句中使用以下重写:

rewrite ^(.*)/index.html$ $1 permanent;

单独使用此功能可用于大多数URL(例如 http://foo.com/bar/index.html),但会破坏 http://foo.com/index.html。要解决此问题,我有以下附加规则:
location = /index.html {
rewrite ^ / permanent;
try_files /index.html =404;
}

找不到文件时, =404部分返回404错误。

我不知道为什么仅靠第一次重写是不够的。

关于$ request_uri中的nginx : rewrite rule to remove/index. html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5675743/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com