首页 > 解决方案 > 使用 NGINX 从链接中删除 .html

问题描述

我的 nginx 站点可用文件有这个配置。它会创建一个链接,例如https://website.com/news-id.html

我想要做的是从链接中删除“news-”和 .html 并使用https://website.com/id使 url 干净

我尝试了几种方法,但每次遇到重定向循环。

**我尝试了几次将代码放在这里但失败了。所以这里是配置文件的链接https://jpst.it/1AKIf

标签: htmlnginxnginx-locationnginx-config

解决方案


试试这个配置。

server {
    location / {

        # Redirects to the version without .html
        if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }

        # Tries the uri, .html file and the news prefix.
        try_files $uri $uri/ $uri.html news-$uri news-$uri/
    }
}

推荐阅读