使用NGINX从链接中删除.html [英] Removing .html from a link using NGINX

查看:541
本文介绍了使用NGINX从链接中删除.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的nginx网站可用文件进行了此配置.它将创建一个类似于 https://website.com/news-id.html 的链接

我想做的是从链接中删除"news-"和.html并使用 https://website.com/id

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

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

解决方案

尝试此配置.

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/
    }
}

I've this configuration for my nginx site-available file. It creates a link like https://website.com/news-id.html

What i want to do is to remove "news-" and .html from the link and make the url clean with just https://website.com/id

I have tried several methods but everytime i run into a redirection loop.

**I tried several times to put the code here but failed. So here is the link to the config file https://jpst.it/1AKIf

解决方案

Try this 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/
    }
}

这篇关于使用NGINX从链接中删除.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆