NGINX:永久删除 url 的一部分

NGINX: remove part of url permanantly

我重新设计了一个网站并更改了 url 格式。 现在我需要把旧的 url 换成新的

这是我的旧 url:

http://www.example.com/forum/showPost/2556/Urgent-Respose

新的 url 将是:

http://www.example.com/2556/Urgent-Respose

如何通过从 url 中删除 /forum/showPost 使用 nginx 重定向到新的 url?

已编辑: 还有这个 url:

http://www.tikshare.com/business/showDetails/1/Pulkit-Sharma-and-Associates,-Chartered-Accountants-in-Bangalore

新 url:

http://www.tikshare.com/classifieds/1/Pulkit-Sharma-and-Associates,-Chartered-Accountants-in-Bangalore

上面的 link 是完全删除,而这个 link 是用 classifieds

替换 business/showDetails

有多种选择。您可以保护位置块中的重写,这将非常有效,因为仅当 URI 前缀匹配时才测试正则表达式:

location ^~ /forum/showPost {
    rewrite ^/forum/showPost(.*)$  permanent;
}

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

您在问题中使用了 permanent - 这会生成 301 响应。

如果您使用 redirect 而不是 permanent - 将生成 302 响应。

如果您使用 last 而不是 permanent - 将发生内部重定向并且浏览器地址栏将继续显示旧的 URL。

回复您的评论:

rewrite ^/forum/showPost(.*)$ /post permanent;
server 
{
    listen 80; ## Listen on port 80 ##
    server_name example.com;  ## Domain Name ##
    index index.html index.php;  ## Set the index for site to use ##
    charset utf-8; ## Set the charset ##
    location ^~ /forum/showPost {
        rewrite ^/forum/showPost(.*)$  permanent;
    }
    location ^~ /business/showDetails {     
        rewrite ^(.*)business/showDetails(.*)$ classifieds permanent;
    }
}