我想在每个以“/”结尾的文档中添加“.html”,但主页除外。
我尝试了一些不同的方法,使用 nginx 重写和返回 301,但我没有让它工作。附上我做的最后一个版本,它正在做/*/.html 但第二个/不应该在那里。
location / {
try_files $uri $uri/ =404;
return 301 $scheme://$host$request_uri.html;
}
我正在寻找的解决方案:
root: domain.com 应该作为 www.domain.com 交付
文件应该被重定向 (301) 到 *.html 版本
- domain.com/abc/到 domain.com/abc.html
- domain.com/dir1/abc/到 domain.com/dir1/abc.html
最佳答案
您将需要使用正则表达式来捕获尾随 /
之前的 URI 部分,以便消除它。一种解决方案是使用带有 try_files
语句的命名位置。
例如:
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^(.+)/$ $1.html permanent;
}
参见 this document了解更多。
关于redirect - nginx redirect :/*/to/*. html (add .html to url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48043048/