代码之家  ›  专栏  ›  技术社区  ›  marketom

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

  •  2
  • marketom  · 技术社区  · 7 年前

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

    我尝试了一些不同的方法,在nginx中使用rewrite和return301,但没有成功。附上我做的最后一个版本,它正在做/*/。html,但第二个/不应存在。

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

    我正在寻找的解决方案:

    根:域。com应作为www.domain交付。通用域名格式

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

    • 领域com/abc/to域。com/abc。html
    • 领域com/dir1/abc/to域。com/dir1/abc。html
    1 回复  |  直到 7 年前
        1
  •  4
  •   Richard Smith    7 年前

    您需要使用正则表达式来捕获URI的一部分 之前 尾部 / ,以消除它。一种解决方案是将命名位置与 try_files 陈述

    例如:

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

    看见 this document 更多信息。