nginx 重定向:/*/ 到 /*.html(将 .html 添加到 url)

nginx redirect: /*/ to /*.html (add .html to url)

我想在每个以“/”结尾的文档中添加“.html”,但主页除外。

我尝试了一些不同的方法,使用重写和 return 301 与 nginx,但我没有让它工作。附上我做的最后一个版本,它正在做 /*/.html 但第二个 / 不应该在那里。

location / {
    try_files $uri $uri/ =404;
    return 301 $scheme://$host$request_uri.html;
}

我正在寻找的解决方案:

root:domain.com 应作为 www.domain.com

传送

文件应重定向 (301) 到 *.html 版本

您将需要使用正则表达式来捕获 URI 尾部 / 的部分,以便消除它。一种解决方案是使用带有 try_files 语句的命名位置。

例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^(.+)/$ .html permanent;
}

有关更多信息,请参阅 this document